blob: 8942c3eeedeaab615112c30242e8fbd88bd6290d [file] [log] [blame]
<html>
<head>
<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel="stylesheet" href="../../../../../default_style.css" type="text/css">
<link rel="stylesheet" href="../../../../../webtools/wtp.css" type="text/css">
<title>jst j2ee</title>
</head>
<body>
<table width="100%" cellspacing="5" cellpadding="2" border="0">
<tbody>
<tr>
<td width="60%" align="left"><font class="indextop">WTP Tutorials - Testing the Creation of Annotated and Non-Annotated Servlets in the
Web Tools Project</font>
<br>
<font class="indexsub">jst j2ee</font></td><td width="40%"><img width="207" hspace="50" height="129" align="middle" src="../../../../../webtools/images/wtplogosmall.jpg"></td>
</tr>
</tbody>
</table>
<table width="100%" cellspacing="5" cellpadding="2" border="0">
<col width="16">
<col width="*">
<tbody>
<tr>
<td valign="top" align="right">&nbsp;</td><td valign="top">
<P>
<B>John Lanuti </B>
<BR>
IBM Rational <BR>
February 24,2005 <BR>
<BR>
<BR>
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. <BR>
</P>
</td>
</tr>
<tr>
<td valign="top" bgcolor="#0080c0" align="left" colspan="2"><b><font face="Arial,Helvetica" color="#ffffff">Prerequisites For The Tutorial</font></b></td>
</tr>
<tr>
<td valign="top" align="right">&nbsp;</td><td valign="top">
<OL>
<LI>Web Tools Platform (WTP) project<BR>The WTP project can be downloaded from <a href="http://download.eclipse.org/webtools/downloads/">http://download.eclipse.org/webtools/downloads/</a>
<BR>
<BR>
</LI>
<LI>XDoclet 1.2.2<BR>XDoclet is available from <a href="http://xdoclet.sourceforge.net" target="_top">http://xdoclet.sourceforge.net</a>
<BR>
<BR>
</LI>
<LI>Tomcat Server<BR>Tomcat is available from <a href="http://jakarta.apache.org/tomcat/" target="_top">http://jakarta.apache.org/tomcat/</a>
<BR>
<BR>
</LI>
<LI>JDK 1.4.2<BR>Sun's JDK is available from <a href="http://java.sun.com/j2se/1.4.2/download.html" target="_top">http://java.sun.com/j2se/1.4.2/download.html</a>
<BR>
<BR>
</LI>
</OL>
</td>
</tr>
<tr>
<td valign="top" bgcolor="#0080c0" align="left" colspan="2"><b><font face="Arial,Helvetica" color="#ffffff">Workspace Configuration </font></b></td>
</tr>
<tr>
<td valign="top" align="right">&nbsp;</td><td valign="top">
<BR>
</td>
</tr>
<tr>
<td valign="top" align="right">&nbsp;</td><td valign="top">
<OL>
<LI>Open the J2EE Perspective. Window-&gt;Open Perspective-&gt;Other...-&gt;J2EE.</LI>
<LI>Set up XDoclet preferences. Window-&gt;Preferences-&gt;J2EE Annotations-&gt;XDoclet.
Make sure the "Enable XDoclet Builder" option
is checked. Select your desired version level and navigate to your XDoclet home directory. Hit OK. <BR>
<BR>
<IMG src="images/ServletScenario_Image22.jpg"><BR>
<BR>
</LI>
<LI>Add the Tomcat server. Window-&gt;Preferences-&gt;Server-&gt;Installed Runtimes.
Hit the "Add" button. <BR>
<BR>
<IMG src="images/ServletScenario_Image14.jpg"><BR>
<BR>
</LI>
<LI>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.<BR>
<BR>
<IMG src="images/ServletScenario_Image13.jpg"><BR>
<BR>
</LI>
</OL>
</td>
</tr>
<tr>
<td valign="top" align="right">&nbsp;</td><td valign="top">
<BR>
</td>
</tr>
<tr>
<td valign="top" align="right">&nbsp;</td><td valign="top">
<BR>
</td>
</tr>
<tr>
<td valign="top" bgcolor="#0080c0" align="left" colspan="2"><b><font face="Arial,Helvetica" color="#ffffff">Dynamic Web Project Creation</font></b></td>
</tr>
<tr>
<td valign="top" align="right">&nbsp;</td><td valign="top">
<BR>
</td>
</tr>
<tr>
<td valign="top" align="right">&nbsp;</td><td valign="top">
<BR>
</td>
</tr>
<tr>
<td valign="top" align="right">&nbsp;</td><td valign="top">
<OL>
<LI>From the J2EE Project Explorer, right click on the "Dynamic Web Projects" group.
Select New-&gt;Dynamic Web Project. <BR>
<BR>
<IMG src="images/ServletScenario_Image1.jpg"> <BR>
<BR>
</LI>
<LI>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. <BR>
<BR>
<IMG src="images/ServletScenario_Image2.jpg"><BR>
<BR>
</LI>
<LI>Ensure the appropriate servlet version is selected to match your level of Tomcat.
Hit finish.</LI>
</OL>
</td>
</tr>
<tr>
<td valign="top" align="right">&nbsp;</td><td valign="top">
<BR>
</td>
</tr>
<tr>
<td valign="top" align="right">&nbsp;</td><td valign="top">
<BR>
</td>
</tr>
<tr>
<td valign="top" bgcolor="#0080c0" align="left" colspan="2"><b><font face="Arial,Helvetica" color="#ffffff"> Non Annotated Servlet Creation </font></b></td>
</tr>
<tr>
<td valign="top" align="right">&nbsp;</td><td valign="top">
<BR>
</td>
</tr>
<tr>
<td valign="top" align="right">&nbsp;</td><td valign="top">
<OL>
<LI>Expand the "Dynamic Web Projects" group to the HelloWorld web project,
and then to the "Servlets" catgeory.</LI>
<LI>Right click on "Servlets" and select New-&gt;Servlet. <BR>
<BR>
<IMG src="images/ServletScenario_Image3.jpg"><BR>
<BR>
</LI>
<LI>Type "test" in as the default java package. Type the Servlet Name, "HelloWorld".
Uncheck the "Generate an annotated servlet class" checkbox. Hit next. <BR>
<BR>
<IMG src="images/ServletScenario_Image5.jpg"><BR>
<BR>
</LI>
<LI> Take all the defaults. Hit next. <BR>
<BR>
<IMG src="images/ServletScenario_Image4.jpg"><BR>
<BR>
</LI>
<LI>Select the doDelete and doPut method checkboxes in addition to the defaults. <BR>
<BR>
<IMG src="images/ServletScenario_Image6.jpg"><BR>
<BR>
</LI>
<LI>Hit "Finish" to create the non annotated servlet. </LI>
<LI>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.<BR>
<BR>
<IMG src="images/ServletScenario_Image7.jpg"><BR>
<BR>
</LI>
<LI>The servlet java class should also show up with the methods defined
that were selected in the wizard. <BR>
<BR>
<IMG src="images/ServletScenario_Image8.jpg"><BR>
<BR>
</LI>
<LI>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:<BR>
<P> PrintWriter out = new PrintWriter(System.out); <BR>
out = response.getWriter(); <BR>
out.println("Hello world!");<BR>
out.close(); </P>
</LI>
<LI>Save and close the editor.</LI>
<BR>
<BR>
</OL>
</td>
</tr>
<tr>
<td valign="top" bgcolor="#0080c0" align="left" colspan="2"><b><font face="Arial,Helvetica" color="#ffffff">Annotated Servlet Creation </font></b></td>
</tr>
<tr>
<td valign="top" align="right">&nbsp;</td><td valign="top">
<BR>
</td>
</tr>
<tr>
<td valign="top" align="right">&nbsp;</td><td valign="top">
<BR>
</td>
</tr>
<tr>
<td valign="top" align="right">&nbsp;</td><td valign="top">
<OL>
<LI>Expand the "Dynamic Web Projects" group to the
HelloWorld web project, and then to the "Servlets" catgeory.</LI>
<LI>Right click on "Servlets" and select New-&gt;Servlet. <BR>
<BR>
<IMG src="images/ServletScenario_Image3.jpg"><BR>
<BR>
</LI>
<LI>Type "test" in as the default java package. Type the Servlet Name,
"XDcoletHelloWorld". Make sure "Generate an annotated servlet class"
is checked. Hit next. <BR>
<BR>
<IMG src="images/ServletScenario_Image11.jpg"><BR>
<BR>
</LI>
<LI> Take the defaults. Hit next. <BR>
<BR>
<IMG src="images/ServletScenario_Image23.jpg"><BR>
<BR>
</LI>
<LI>Select the doDelete and doPut method checkboxes in addition to the defaults. <BR>
<BR>
<IMG src="images/ServletScenario_Image6.jpg"><BR>
<BR>
</LI>
<LI>Hit "Finish" to create the annotated servlet.</LI>
<LI>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.<BR>
</LI>
<LI>The servlet java class should also show up with the methods
defined that were selected in the wizard. <BR>
<BR>
<IMG src="images/ServletScenario_Image15.jpg"><BR>
<BR>
</LI>
<LI>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:<BR>
<P> PrintWriter out = new PrintWriter(System.out); <BR>
out = response.getWriter(); <BR>
out.println("Hello world!");<BR>
out.close(); </P>
</LI>
<LI>Save and close the editor.</LI>
</OL>
</td>
</tr>
<tr>
<td valign="top" align="right">&nbsp;</td><td valign="top">
<BR>
</td>
</tr>
<tr>
<td valign="top" align="right">&nbsp;</td><td valign="top">
<BR>
</td>
</tr>
<tr>
<td valign="top" bgcolor="#0080c0" align="left" colspan="2"><b><font face="Arial,Helvetica" color="#ffffff">Running the Servlets on the Tomcat Server </font></b></td>
</tr>
<tr>
<td valign="top" align="right">&nbsp;</td><td valign="top">
<BR>
</td>
</tr>
<tr>
<td valign="top" align="right">&nbsp;</td><td valign="top">
<OL>
<LI>Select the "HelloWorld" web project. Right click and select
Run As-&gt;Run on Server... <BR>
<BR>
<IMG src="images/ServletScenario_Image16.jpg"><BR>
<BR>
</LI>
<LI>Choose to manually define a new Server. Select the Apache Tomcat version
you have installed. Hit next. <BR>
<BR>
<IMG src="images/ServletScenario_Image17.jpg"><BR>
<BR>
</LI>
<LI>Ensure the Helloworld.war is added to the server. <BR>
<BR>
<IMG src="images/ServletScenario_Image18.jpg"><BR>
<BR>
</LI>
<LI>Hit Finish.</LI>
<LI>Make sure if you double click the Tomcat server instance that the
HelloWorld project is configured correctly. Edit and restart. <BR>
<BR>
<IMG src="images/ServletScenario_Image21.jpg"> <BR>
<BR>
</LI>
<LI>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. <BR>
<BR>
<IMG src="images/ServletScenario_Image19.jpg"><BR>
<BR>
</LI>
<LI>Now try the annotated servlet, http://localhost:8080/HelloWorld/XDocletHelloWorld. <BR>
<BR>
<IMG src="images/ServletScenario_Image20.jpg"><BR>
<BR>
</LI>
</OL>
</td>
</tr>
</tbody>
</table>
</body>
</html>