Example 1: A servlet
Contents
Now it is time to make use of the container. You will have to create a dynamic web app (Eclipse Project) with only a single servlet. On request the servlet creates a database connection and shows a list of actors from the sakila database.
package de.neuromechanics; import java.io.IOException; import java.io.PrintWriter; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import javax.annotation.Resource; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.sql.DataSource; /** * Servlet implementation class DatasourceServlet */ @WebServlet("/myds") public class DatasourceServlet extends HttpServlet { private static final long serialVersionUID = 1L; @Resource(name="sakilaDS") private DataSource ds; /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { final PrintWriter writer = response.getWriter(); response.setContentType("text/html; charset=UTF-8"); writer.println("<!DOCTYPE html>"); writer.println("<html>"); writer.println("<body>"); writer.println("<table border='1'>"); writer.println("<tr>"); writer.println("<th>Lastname</th>"); writer.println("<th>ID</th>"); writer.println("</tr>"); Connection con; try { con = ds.getConnection(); Statement stmt = con.createStatement(); ResultSet rs = stmt.executeQuery("SELECT last_name, actor_id FROM actor"); while (rs.next()) { String name = rs.getString("last_name"); String id = rs.getString("actor_id"); writer.println("<tr>"); writer.println("<td>" + name + "</td>"); writer.println("<td>" + id + "</td>"); writer.println("</tr>"); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } writer.println("</table></body></html>"); } }
For this to work we need a resource reference to the sakila datasource. For Wildfly/JBoss this is done via the jboss-web.xml in the WEB-INF folder of the application.
<?xml version="1.0" encoding="UTF-8"?> <jboss-web> <resource-ref> <res-ref-name>sakilaDS</res-ref-name> <jndi-name>java:/jdbc/datasources/sakilaDS</jndi-name> </resource-ref> </jboss-web>
The final step will be to create a deployable web archive (.war) file and copy it into the folder which we share with the wildfly container. To do so, we must “Export” our Eclipse project to a WAR file. In Eclipse project explorer right-click on the project’s top entry and select “Export”. After you selected “WAR file” you’re asked for a destination into which to put the war-file. Here we specify the exact same path we chose in the -v parameter when we created the wildfly container (-v C:\[CUSTOM_PATH]\JBoss_deployments ….. Hopefully you didn’t forget to replace [CUSTOM_PATH] with the path you’ve chosen at that time).