Chapter 8: Building J2EE Web Components with Ant and XDoclet


If you take a look at the current Internet software development arena, a good amount of the heavy lifting is still being accomplished with servlets. One of the reasons for this is the ability to use the advanced features of Java to handle the processing of information to and from the user . There are many commercial and open -source application servers to handle the servlets as well. One of the most important issues of servlet development is the creation of the web.xml file, which is the deployment descriptor for the Web application. Fortunately, XDoclet allows the automatic generation of the web.xml file using the webdoclet Ant task. In addition, we can use XDoclet to further configure the servlet using filtering and custom tags or tablibs.

Servlet Example

We will use a step-by-step process to show how to use XDoclet with servlets by creating a simple servlet and deploying it on an application server. For this example, we will be using Resin as the application server. First we will create a simple servlet, as shown inthe following listing. This servlet will query an account database, display the information in a table, and output some other simple text.

 package example; import javax.servlet.*;  import javax.servlet.http.*;  import javax.sql.*;  import java.sgl.*;  import javax.naming.*; public class SimpleServlet extends HttpServlet { public void init(ServletConfig config)  throws ServletException {  super.init(config); } protected void handleUser(HttpServletRequest request,  HttpServletResponse response)  throws ServletException }     try {       response.setContentType("text/html");        java.io.PrintWriter out = response.getWriter();       out.println("<html>");       out.println("<head>");       out.println("<title>My Simple Servlet" ),              out.println("</head>");        out.println("<body>");       out.println("The current database is Production<br>");       out.println("The username is 'accounts'<br><br>");        displayTable(out);       out.print In ("</body>");        out.println("</html>");        out.close();     } catch (Exception e) {       throw new ServletException (e);     }  }  protected void doGet(HttpServletRequest request,                        HttpServletResponse response)                        throws ServletException {       handleUser(request, response);  }  protected void doPost(HttpServletRequest request,                         HttpServletResponse response)                         throws ServletException {     handleUser(request, response);  }  private void displayTable(java.io.PrintWriter out)                               throws Exception {       Object obj = new InitialContext().lookup("java:comp/en v/jdbc/dbconnection");            DataSource pool = (DataSource) obj;       if (pool != null) {         Connection connection = pool.getConnetion();         out.println("<table>");         out.println("<tr><td>description</td><td>price</td></tr>" );          try {           Statement statement = connection.createStatement();           ResultSet rs = statement.executeQuery(              "SELECT * FROM production WHERE username="accounts");           while(rs.nextQ) {             out.println("<tr><td>" + rs.getString("description") +"</td><td>" +                          rs.getString(`price") + "</td></tr>");           }         } finally {           connection.close();         }          out.println("</table>");       }       } } 

For this servlet we will need to create a web.xml deployment descriptor, as shown in the following listing.

 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.   //DTD Web Application 2.3//EN" http://Iava.sun.comldtd/web-app 2 3.dtd> <web-app>   <servlet servlet-name="SimpleServlet"             servlet-class="example. SimpleServlet"   />   <load-on-startup>1</load-on-startup>   <servlet-mapping>     <servlet-name>SimpleServlet</servlet-name>      <url-pattern>/Example/*</url-pattern >    </servlet-mapping>   <servlet-mapping>     <servlet-name>SimpleServlet</servlet-name>      <url-pattern>SimpleServlet</url-pattern>    </servlet-mapping>   <resource-ref>     <description>JDBC Connection</description>      <res-ref-name>jdbc/dbconnection</res-ref-name>      <res-type>javax.sgl.DataSource</res-type>      <res-auth>Container</res-auth>   </resource-ref> </web-app> 

If we deploy the code in the two previous listings on an application server and place the appropriate database table in our database, we will see an output page with the contents of the database table. Now, let's consider what the code is doing, even though it is just an example.

The primary purpose of this code example is to pull various rows from a database table based on an account name. During a production run, we want the table used to be called Production and have a username of Accounts. However, during development or testing work, we don't want to mess with trying to find the appropriate places within the code to make the changes. Instead, we'd like a common location.

Even more important, the deployment descriptor must be specifically designed for this servlet. There should be a way to automatically create the deployment descriptor from the servlet source code in order to keep like code with like code. We can accomplish both tasks using webdoclet XDoclet tags. As you know, XDoclet tags are added to code within the comments. So, we will change the code in the first listing to include a comment section with the XDoclet tags for the deployment descriptor and also change the code to use init-param tags for our database table and username. This following listing shows the new code.

 package example; /** *    @version 0.5 *    @web.servlet name="SimpleServlet" *             display-name="Simple Servlet" *             load-on-startup=" l" * *    @web-servlet-init-param name="table" value="production" *    @web-servlet-init-param name="account" value="accounts" * *    @web.resource-ref description="database connection" *                  name="jdbc/dbconnection" *                      type="javax.sql.DataSource" *                  auth="Container" * *    @web.servlet-mapping url-pattern ="/Example/*" *    @web.servlet-mapping url-pattern ="/SimpleServlet" */ import javax.servlet.*;  import javax.servlet.http.*;  import javax.sgl.*;  import java.sgl.*;  import javax.naming.*; public class SimpleServlet extends HttpServlet {   public void init(ServletConfig config)                     throws ServletException {      super.init(config);   }   protected void handleUser(HttpServletRequest request,                              HttpServletResponse response)                              throws ServletException {     try {       response.setContentType("text/html");        java.io.PrintWriter out = response.getWriter();       out.println("<html>");       out.println("<head>");       out.println("<title>My Simple Servlet");        out.println("</head>");        out.println("<body>");       out.println("The current database is Production<br>");        out.println("The username is `accounts'<br><br>");        displayTable(out);       out.println("</body>");        out.println("</html>");        out.close();     } catch (Exception e) {       throw new ServletException (e);     }   }   protected void doGet(HttpServletRequest request,                        HttpServletResponse response)                        throws ServletException {     handleUser(request, response);   }   protected void doPost(HttpServletRequest request,                         HttpServletResponse response)                         throws ServletException {     handleUser(request, response);   }   private void displayTable(java.io.PrintWriter out)                             throws Exception {     ServletConfig config = this.getServletConfig();     String table = config.getInitParameter("table");     String username = config.getln1tParameter("account");     Object obj = new InitialContext().lookup("java:comp/env/jdbc/dbconnection");          DataSource pool = (DataSource) obj;     if (pool != null) {       Connection connection = pool.getConnection();       out.println("<table>");       out.print ln("<tr><td>description</td><td>price</td><Itr>");        try {         Statement statement = connection.createStatementO;         ResultSet rs = statement.executeQuery(           "SELECT * FROM " + table + " WHERE username="' + username + ""');               while(rs.next()) {           out.println("<tr><td>" + rs.getString("description") +"</td><td>" +                        rs.getString("price") + "</td></tr>");         }       } finally {          connection.close();       }       out.println("</table>");      }   } } 

There are two primary changes in this listing. The first is the comment section, and we will discuss that section in a moment. The second change occurred in the displayTable() method. In the function, we now pull the table and account name to use in the database query directory from the deployment descriptor because they will be declared as init parameters.




Professional Java Tools for Extreme Programming
Professional Java Tools for Extreme Programming: Ant, XDoclet, JUnit, Cactus, and Maven (Programmer to Programmer)
ISBN: 0764556177
EAN: 2147483647
Year: 2003
Pages: 228

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net