Using Eclipse with Webdoclet


Clearly the use of XDoclet makes the development of traditional Web-based applications easy and less prone to coding errors. When developing applications, there are those who use typical text-based editors such as vi and emacs and those who use an IDE. We aren't here to say which is more appropriate, but in this section we will show how the combination of Eclipse, Ant, and XDoclet can make building servlets even easier. We will use code from the third listing in this chapter.

Before we begin the process of building a new project, you need to make sure that you have Eclipse installed. For testing this code, we used Eclipse 2.1.1 and Tomcat. You will need to install the myeclipse plug-in found at www.myeclipseide.com. You will also need to register with the site in order to download the myeclipse plug-in.

Launch Eclipse to start the new project. The myeclipse plug-in will install itself as a perspective. Open the perspective by choosing Window > Open Perspective > Other > MyEclipse. Select File > New > Project > J2EE > Web Module Project and then click Next . Enter a project name of SimpleProject , and enter /SimpleProject for the Context root URL. You should see the dialog shown in the following figure.

click to expand

Click the Finish button to see a new project layout, as shown in this figure:

click to expand

Now we need to create a new servlet. Click the SimpleProject entry in the Package Explorer and choose File > New > Servlet. In the Name field enter the value SimpleServlet . In the Package field enter the value example . Your dialog should appear as shown in the next figure.

click to expand

Click the Next button. The next wizard dialog will have the Generate/Map web.xml file checked. Uncheck this box because we want to use XDoclet to create our web.xml file. The dialog should now appear as shown in this figure:

click to expand

Click the Finish button to allow the IDE to create the servlet. You will be presented with an open Edit pane. Add the following code to the Edit pane:

 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 usemame = 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>");      }   } } 

Now select the SimpleProject folder in the Package Explorer, and select File > New > Folder. In the Folder Name field, enter a value of build , as shown in the following figure.

click to expand

Click the Finish button. Then choose the SimpleProject/Merge entry in the Package Explorer, and select File > New > XML. Populate the dialog with field File Name: welcomefile.xml as shown in the next figure, and click Finish. When the file opens, replace the contents with the following:

 <welcome-file-list>  <welcome-file>index.html</welcome-file> </welcome-file-list> 
click to expand

Save the file. Now select the SimpleProject folder in the Package Explorer, and select File > New > File. In the File Name field, enter the name xdoclet-build.properties , as shown in the next figure, and click Finish.

click to expand

Replace the contents of the file with the following:

 table = production account = accounts 

Now we need to deal with the XDoclet configuration. Right-click the SimpleProject folder in the Package Explorer. Select Properties > XDoclet Configurations to bring up the Properties dialog shown in the next figure.

click to expand

Right-click the dialog and select Add Standard; in this dialog select Standard Web. Your dialog should now look like this:

click to expand

Select the deploymentdescriptor option in the list, and set the following values:

  • Servletspec = 2.3

  • destDir = WebRoot/WEB-INF

  • displayName = Simple Project

  • mergeDir = build

Your dialog should now look like this:

click to expand

Now click OK to create an xdoclet-build.xml folder.

Next we need to set up Ant. Right-click the xdoclet-build.xml file in Package Explorer and select Run Ant. In the resulting dialog, choose the Properties tab. In the Property Files list, click the Add button. Browse to the SimpleProject folder and select xdoclet-build.properties. Your dialog should look like this:

click to expand

Click the Refresh tab and be sure that both checkboxes are checked. Click the Apply button and then click the Run button to let XDoclet do its job, and the web.xml file will be created.

Finally, let's add an HTML page for our servlet. Choose the SimpleProject folder in Package Explorer; right-click and select New > HTML. In the File Name field, enter the value index.html and click Finish. Add the following HTML to the opened page:

 <html>   <head>     <title>Simple Project</title>   </head>   <body>     <br />       <h3>Simple project</h3>         <a href="SimpleServlet">Test Servlet</a>   </body> </html> 

Save the file. Now let's set up the deployment. Right-click the SimpleProject project and select MyEclipse. Then choose Add And Remove Project Deployments. Click the Add button in the resulting dialog and select your server, as shown in the following figure.

click to expand

Click the Finish button and then click OK. Select the server and start it. Open a browser and test your servlet.




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