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 directory—the root directory for compiled servlet code—and click OK, bringing up the New Folder dialog again. In that folder, give this new folder the name output, as you see in Screenshot-5, and click OK.

Screenshot-5. Creating a new output folder
Java 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 NewScreenshot 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 Screenshot-6.

Screenshot-6. Using a linked folder
Java 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 elements—those 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 "-//Oracle//DTD Web app 2.3//EN" "http://java.oracle.com/dtd/web-app_2_3.dtd">
<web-app>
 <display-name>Example apps</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 directories—just navigate to the new servlet's URL, http://localhost:8080/Ch09/org.eclipsebook.ch09.Ch09_03, as you see in Screenshot-7.

Screenshot-7. Developing a servlet in place
Java figs/ecps_0907.gif

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

      
Comments