|
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 |
|
- Web Tools Platform (WTP) project
The WTP project can be downloaded from http://download.eclipse.org/webtools/downloads/
- XDoclet 1.2.2
XDoclet is available from http://xdoclet.sourceforge.net
- Tomcat Server
Tomcat is available from http://jakarta.apache.org/tomcat/
- JDK 1.4.2
Sun's JDK is available from http://java.sun.com/j2se/1.4.2/download.html
|
Workspace Configuration |
|
|
|
- Open the J2EE Perspective. Window->Open Perspective->Other...->J2EE.
- 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.
- Add the Tomcat server. Window->Preferences->Server->Installed Runtimes. Hit the "Add"
button.
- 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 |
|
|
|
|
|
- From the J2EE Project Explorer, right click on the "Dynamic Web Projects" group. Select
New->Dynamic Web Project.
- 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.
- Ensure the appropriate servlet version is selected to match your level of Tomcat. Hit finish.
|
|
|
|
|
Non Annotated Servlet Creation |
|
|
|
- Expand the "Dynamic Web Projects" group to the HelloWorld web project, and then to the
"Servlets" catgeory.
- Right click on "Servlets" and select New->Servlet.
- Type "test" in as the default java package. Type the Servlet Name, "HelloWorld". Uncheck the
"Generate an annotated servlet class" checkbox. Hit next.
- Take all the defaults. Hit next.
- Select the doDelete and doPut method checkboxes in addition to the defaults.
- Hit "Finish" to create the non annotated servlet.
- 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.
- The servlet java class should also show up with the methods defined that were selected in the
wizard.
- 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();
- Save and close the editor.
|
Annotated Servlet Creation |
|
|
|
|
|
- Expand the "Dynamic Web Projects" group to the HelloWorld web project, and then to the
"Servlets" catgeory.
- Right click on "Servlets" and select New->Servlet.
- Type "test" in as the default java package. Type the Servlet Name, "XDcoletHelloWorld". Make
sure "Generate an annotated servlet class" is checked. Hit next.
- Take the defaults. Hit next.
- Select the doDelete and doPut method checkboxes in addition to the defaults.
- Hit "Finish" to create the annotated servlet.
- 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.
- The servlet java class should also show up with the methods defined that were selected in the
wizard.
- 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();
- Save and close the editor.
|
|
|
|
|
Running the Servlets on the Tomcat Server |
|
|
|
- Select the "HelloWorld" web project. Right click and select Run As->Run on Server...
- Choose to manually define a new Server. Select the Apache Tomcat version you have installed.
Hit next.
- Ensure the Helloworld.war is added to the server.
- Hit Finish.
- Make sure if you double click the Tomcat server instance that the HelloWorld project is
configured correctly. Edit and restart.
- 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.
- Now try the annotated servlet, http://localhost:8080/HelloWorld/XDocletHelloWorld.
|