Recipe 11.5 Installing a Servlet in Tomcat


11.5.1 Problem

You've created a servlet .class file, and you want to run it in Tomcat.

11.5.2 Solution

Place the .class file into the correct Tomcat directory (such as webapps\Ch11\WEB-INF\class\org\cookbook\ch11 ). Add servlet data to the web.xml file, restart Tomcat, and navigate to your servlet's URL, such as http://localhost:8080/ch11/org.cookbook.ch11.ServletClass .

11.5.3 Discussion

To install a .class file, you can simply copy it over to the correct directory in the Tomcat directory structure. Because the servlet is in the org.cookbook.ch11 package, and the directory structure must mirror the package structure, put ServletClass.class in webapps\Ch11\WEB-INF\class\org\cookbook\ch11 :

 webapps _  _ch11                            _  _WEB-INF                          _  _classes                        _  _org                 _  _cookbook                     _  _ch11  Put the servlet's code here  _  _lib 

To set up this servlet with Tomcat, we'll create the deployment descriptor file, web.xml . In this XML file, we'll use two elements, <servlet> and <servlet-mapping> , to register this servlet with Tomcat. The web.xml file we'll use appears in Example 11-3.

Example 11-3. web.xml
 <?xml version="1.0" encoding="ISO-8859-1"?> <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"                                          ""> <web-app>   <display-name>Example Applications</display-name>   <servlet>     <servlet-name>Servlet</servlet-name>     <servlet-class>org.cookbook.ch11.ServletClass</servlet-class>   </servlet>   <servlet-mapping>     <servlet-name>Servlet</servlet-name>     <url-pattern>/org.cookbook.ch11.ServletClass</url-pattern>   </servlet-mapping> </web-app> 

Right-click the Servlet project to create this new XML document and select New File. To open web.xml in Eclipse, right-click it and select Open With Text Editor (unless you have a dedicated XML editor installed in Eclipse, as with a plug-in, Eclipse opens your XML document in your default XML editor, which is probably a web browser). Save the file, and copy it to the ch11 directory's WEB-INF directory:

 webapps _  _ch11                            _  _WEB-INF  Store web.xml here  _  _classes                     _  _lib 

Then (re)start Tomcat, giving it a chance to copy everything over to the work directory. Now navigate to http://localhost:8080/ch11/org.cookbook.ch11.ServletClass in a browser. You should see the new servlet running, as shown in Figure 11-4.

Figure 11-4. A working servlet

We're creating functional compiled servlet code with Eclipse at this point. But there's no denying it's a pain to have to copy files from the Eclipse directories to the Tomcat directories. Take a look at the following recipes to see how you can work with files in the Tomcat directories in Eclipse directly.

11.5.4 See Also

Recipe 11.6 on creating a servlet in place; Recipe 11.7 on editing web .xml in place; Chapter 2 and Chapter 3 in Java Servlet Programming (O'Reilly); Chapter 2 in the Java Servlet and JSP Cookbook (O'Reilly).

Eclipse Cookbook
Inside XML (Inside (New Riders))
ISBN: 596007108
EAN: 2147483647
Year: 2006
Pages: 232
Authors: Steve Holzner

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: