WTP Tutorials - Testing the Creation of Annotated and Non-Annotated Servlets in the Web Tools Project
jst j2ee
 

John Lanuti
IBM Rational
February 24,2005


This tutorial will show you how to set up, create, and deploy a simple Hello World servlet on the Tomcat server using the Eclipse Web Tools Project.

Prerequisites For The Tutorial
 
  1. Web Tools Platform (WTP) project
    The WTP project can be downloaded from http://download.eclipse.org/webtools/downloads/

  2. XDoclet 1.2.2
    XDoclet is available from http://xdoclet.sourceforge.net

  3. Tomcat Server
    Tomcat is available from http://jakarta.apache.org/tomcat/

  4. JDK 1.4.2
    Sun's JDK is available from http://java.sun.com/j2se/1.4.2/download.html

Workspace Configuration
 
 
  1. Open the J2EE Perspective. Window->Open Perspective->Other...->J2EE.
  2. Set up XDoclet preferences. Window->Preferences->J2EE Annotations->XDoclet. Make sure the "Enable XDoclet Builder" option is checked. Select your desired version level and navigate to your XDoclet home directory. Hit OK.



  3. Add the Tomcat server. Window->Preferences->Server->Installed Runtimes. Hit the "Add" button.



  4. Select the appropriate Apache Tomcat level for your Tomcat server and hit the "Next" button. Fill in the appropriate Tomcat home directory. Hit the "Finish" button.



 
 
Dynamic Web Project Creation
 
 
 
  1. From the J2EE Project Explorer, right click on the "Dynamic Web Projects" group. Select New->Dynamic Web Project.



  2. Type the project name, "HelloWorld". Make sure the "Add Module to EAR project" selection is unchecked. Tomcat does not support the use of EAR projects so we will only make a stand alone web application.



  3. Ensure the appropriate servlet version is selected to match your level of Tomcat. Hit finish.
 
 
Non Annotated Servlet Creation
 
 
  1. Expand the "Dynamic Web Projects" group to the HelloWorld web project, and then to the "Servlets" catgeory.
  2. Right click on "Servlets" and select New->Servlet.



  3. Type "test" in as the default java package. Type the Servlet Name, "HelloWorld". Uncheck the "Generate an annotated servlet class" checkbox. Hit next.



  4. Take all the defaults. Hit next.



  5. Select the doDelete and doPut method checkboxes in addition to the defaults.



  6. Hit "Finish" to create the non annotated servlet.
  7. Because it is non-annotated, the web deployment descriptor metadata artifacts will be created for you as well. We can verify their existence by looking in the project explorer "Servlets" section for the HelloWorld web application. The HelloWorld servlet and servlet mapping should now show up.



  8. The servlet java class should also show up with the methods defined that were selected in the wizard.



  9. Double click on the HelloWorld class in the Project Explorer. Add the following code to the doGet method, , as well as an import statement for java.io.PrintWriter:

    PrintWriter out = new PrintWriter(System.out);
    out = response.getWriter();
    out.println("Hello world!");
    out.close();

  10. Save and close the editor.


Annotated Servlet Creation
 
 
 
  1. Expand the "Dynamic Web Projects" group to the HelloWorld web project, and then to the "Servlets" catgeory.
  2. Right click on "Servlets" and select New->Servlet.



  3. Type "test" in as the default java package. Type the Servlet Name, "XDcoletHelloWorld". Make sure "Generate an annotated servlet class" is checked. Hit next.



  4. Take the defaults. Hit next.



  5. Select the doDelete and doPut method checkboxes in addition to the defaults.



  6. Hit "Finish" to create the annotated servlet.
  7. Because it is annotated, the web deployment descriptor metadata artifacts will be created during a build. The servlet annotated tags will be parsed and the xdoclet engine will generate the web.xml servlet nodel. We can verify their existence by looking in the project explorer "Servlets" section for the HelloWorld web application. The XDocletHelloWorld servlet and servlet mapping should now show up.
  8. The servlet java class should also show up with the methods defined that were selected in the wizard.



  9. Double click on the XDocletHelloWorld class in the Project Explorer. Add the following code to the doGet method, as well as an import statement for java.io.PrintWriter:

    PrintWriter out = new PrintWriter(System.out);
    out = response.getWriter();
    out.println("Hello world!");
    out.close();

  10. Save and close the editor.
 
 
Running the Servlets on the Tomcat Server
 
 
  1. Select the "HelloWorld" web project. Right click and select Run As->Run on Server...



  2. Choose to manually define a new Server. Select the Apache Tomcat version you have installed. Hit next.



  3. Ensure the Helloworld.war is added to the server.



  4. Hit Finish.
  5. Make sure if you double click the Tomcat server instance that the HelloWorld project is configured correctly. Edit and restart.



  6. Wait for the Tomcat server to start and the web browser to open. Type http://localhost:8080/HelloWorld/HelloWorld in the address window. Hit enter.



  7. Now try the annotated servlet, http://localhost:8080/HelloWorld/XDocletHelloWorld.