blob: 8649c49852d95ca49badde90440a67c9d57d0a29 [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"
alt=""></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"
alt=""><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"
alt=""><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"
alt=""><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"
alt=""> <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"
alt=""><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"
alt=""><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"
alt=""><br>
<br>
</li>
<li>Take all the defaults. Hit next. <br>
<br>
<img
src="images/ServletScenario_Image4.jpg"
alt=""><br>
<br>
</li>
<li>Select the doDelete and doPut method checkboxes in addition to the defaults. <br>
<br>
<img
src="images/ServletScenario_Image6.jpg"
alt=""><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"
alt=""><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"
alt=""><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"
alt=""><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"
alt=""><br>
<br>
</li>
<li>Take the defaults. Hit next. <br>
<br>
<img
src="images/ServletScenario_Image23.jpg"
alt=""><br>
<br>
</li>
<li>Select the doDelete and doPut method checkboxes in addition to the defaults. <br>
<br>
<img
src="images/ServletScenario_Image6.jpg"
alt=""><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"
alt=""><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"
alt=""><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"
alt=""><br>
<br>
</li>
<li>Ensure the Helloworld.war is added to the server. <br>
<br>
<img
src="images/ServletScenario_Image18.jpg"
alt=""><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"
alt=""> <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"
alt=""><br>
<br>
</li>
<li>Now try the annotated servlet, http://localhost:8080/HelloWorld/XDocletHelloWorld. <br>
<br>
<img
src="images/ServletScenario_Image20.jpg"
alt=""><br>
<br>
</li>
</ol>
</td>
</tr>
</tbody>
</table>
</body>
</html>