blob: c68961877cc08635b6745b93b8aa8b92baf0bbf9 [file] [log] [blame]
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="../../../../wtp.xsl"?>
<html>
<head>
<meta name="root" content="../../../../.." />
<title>jst j2ee</title>
</head>
<body>
<h1>WTP Tutorials - Testing the Creation of Annotated and Non-Annotated Servlets in the
Web Tools Project</h1>
<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>
<h2>Prerequisites For The Tutorial</h2>
<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>
<h2>Workspace Configuration </h2><BR/>
<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 &quot;Enable XDoclet Builder&quot; 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 &quot;Add&quot; 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 &quot;Next&quot; button. Fill in the appropriate Tomcat
home directory. Hit the &quot;Finish&quot; button.<BR/> <BR/>
<IMG src="images/ServletScenario_Image13.jpg" /><BR/><BR/></LI>
</OL> <BR/> <BR/>
<h2>Dynamic Web Project Creation</h2> <BR/> <BR/>
<OL>
<LI>From the J2EE Project Explorer, right click on the &quot;Dynamic Web Projects&quot; group.
Select New-&gt;Dynamic Web Project. <BR/> <BR/>
<IMG src="images/ServletScenario_Image1.jpg" /> <BR/> <BR/></LI>
<LI>Type the project name, &quot;HelloWorld&quot;. Make sure the &quot;Add Module
to EAR project&quot; 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> <BR/> <BR/>
<h2> Non Annotated Servlet Creation </h2><BR/>
<OL>
<LI>Expand the &quot;Dynamic Web Projects&quot; group to the HelloWorld web project,
and then to the &quot;Servlets&quot; catgeory.</LI>
<LI>Right click on &quot;Servlets&quot; and select New-&gt;Servlet. <BR/> <BR/>
<IMG src="images/ServletScenario_Image3.jpg" /><BR/> <BR/> </LI>
<LI>Type &quot;test&quot; in as the default java package. Type the Servlet Name, &quot;HelloWorld&quot;.
Uncheck the &quot;Generate an annotated servlet class&quot; 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 &quot;Finish&quot; 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 &quot;Servlets&quot;
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(&quot;Hello world!&quot;);<BR/>
out.close(); </P></LI>
<LI>Save and close the editor.</LI> <BR/> <BR/>
</OL>
<h2>Annotated Servlet Creation </h2><BR/> <BR/>
<OL>
<LI>Expand the &quot;Dynamic Web Projects&quot; group to the
HelloWorld web project, and then to the &quot;Servlets&quot; catgeory.</LI>
<LI>Right click on &quot;Servlets&quot; and select New-&gt;Servlet. <BR/><BR/>
<IMG src="images/ServletScenario_Image3.jpg" /><BR/><BR/></LI>
<LI>Type &quot;test&quot; in as the default java package. Type the Servlet Name,
&quot;XDcoletHelloWorld&quot;. Make sure &quot;Generate an annotated servlet class&quot;
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 &quot;Finish&quot; 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 &quot;Servlets&quot;
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(&quot;Hello world!&quot;);<BR/>
out.close(); </P></LI>
<LI>Save and close the editor.</LI>
</OL> <BR/> <BR/>
<h2>Running the Servlets on the Tomcat Server </h2> <BR/>
<OL>
<LI>Select the &quot;HelloWorld&quot; 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>
</body>
</html>