9.4 Creating a Servlet in Place

     

The code we're developing needs to be in the Tomcat directories to run, and storing the compiled code we generate there is no problem with Eclipse. To see how this works, create a new project, Ch09_03 . Enter the name of the project in the New Java Project dialog and click Next to bring up the second pane of this dialog. To set the default output folder for compiled code, click the Browse button next to the Default output folder box to open the Folder Selection dialog. Click the Create new folder button, and click the Advanced button in the New Folder dialog. Now select the Link to folder in the filesystem checkbox, and click the Browse button. Browse to the jakarta-tomcat-4.1.29\webapps\Ch09\WEB-INF\classes directorythe root directory for compiled servlet codeand click OK, bringing up the New Folder dialog again. In that folder, give this new folder the name output, as you see in Figure 9-5, and click OK.

Figure 9-5. Creating a new output folder
figs/ecps_0905.gif

Setting up the output folder this way means that the code we compile will automatically be placed in the classes folder (or, in our case, in the classes\org\eclipsebook\ch09 folder, following the package name we're using). Now create a new class, Ch09_03 , and enter the code for the servlet in Example 9-4 in it.

Example 9-4. A new servlet
 import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class ch09_03 extends HttpServlet  {     public void doGet(HttpServletRequest request,         HttpServletResponse response)         throws IOException, ServletException     {         response.setContentType("text/html");         PrintWriter out = response.getWriter( );         out.println("<HTML>");         out.println("<HEAD>");         out.println("<TITLE>");         out.println("A Servlet Example");         out.println("</TITLE>");         out.println("</HEAD>");         out.println("<BODY>");         out.println("<H1>");         out.println("Working With Servlets");         out.println("</H1>");         out.println("Developing servlets in place");         out.println("</BODY>");         out.println("</HTML>");     } } 

Finally, add servlet.jar to the build path as before. When you build this project, Ch09_03.class will automatically be stored in the Tomcat webapps\WEB-INF\classes\org\eclipsebook\ch09 directory, which is exactly where it should go.

We'll also need to edit web.xml to install this new servlet. This can be done in Eclipse using a linked folder . In this case, we're going to link to the Ch09/WEB-INF folder in the Tomcat installation, which holds web.xml . To create a linked folder, right-click the Ch09_03 project and select New Folder. Click the Advanced button in the New Folder dialog, and select the Link to folder in the filesystem checkbox, then click the Browse button and browse to jakarta-tomcat-4.1.29\webapps\Ch09\WEB-INF . Then click OK to bring up the New Folder dialog again, enter the name WEB-INF for this new folder in the Folder Name box, and click OK. This creates a new linked folder named WEB-INF , and you can access the contents of this folder, including web.xml , by opening it in the Package Explorer, as you see in Figure 9-6.

Figure 9-6. Using a linked folder
figs/ecps_0906.gif

The web.xml to use in this project appears in Example 9-5 (note that servlet elements must be grouped together, followed by the grouped servlet-mapping elementsthose elements should not be mixed, or Tomcat won't be able to parse the file, which means it won't start).

Example 9-5. The new version of web.xml
 <?xml version="1.0" encoding="ISO-8859-1"?> <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"  "http://java.sun.com/dtd/web-app_2_3.dtd"> <web-app>   <display-name>Example Applications</display-name>   <servlet>     <servlet-name>Ch09_02</servlet-name>     <servlet-class>org.eclipsebook.ch09.Ch09_02</servlet-class>   </servlet>   <servlet>     <servlet-name>Ch09_03</servlet-name>     <servlet-class>org.eclipsebook.ch09.Ch09_03</servlet-class>   </servlet>   <servlet-mapping>     <servlet-name>Ch09_02</servlet-name>     <url-pattern>/org.eclipsebook.ch09.Ch09_02</url-pattern>   </servlet-mapping>   <servlet-mapping>     <servlet-name>Ch09_03</servlet-name>     <url-pattern>/org.eclipsebook.ch09.Ch09_03</url-pattern>   </servlet-mapping> </web-app> 

Enter this new version of web.xml now, and restart Tomcat. Eclipse has taken care of all the file handling for us, so there's nothing to copy to the Tomcat directoriesjust navigate to the new servlet's URL, http://localhost:8080/Ch09/org.eclipsebook.ch09.Ch09_03 , as you see in Figure 9-7.

Figure 9-7. Developing a servlet in place
figs/ecps_0907.gif

Getting betternow we've developed a servlet's code entirely in Eclipse. Eclipse has even done the file handling for us.



Eclipse
Eclipse
ISBN: 0596006411
EAN: 2147483647
Year: 2006
Pages: 114
Authors: Steve Holzner

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net