< Day Day Up > |
Recipe 11.7 Editing web.xml in Place11.7.1 ProblemYou want to edit web.xml in Eclipse while keeping it in the Tomcat directories. 11.7.2 SolutionLink to needed files such as web.xml in your project. 11.7.3 DiscussionTo get access to web.xml in Eclipse, even though it's in the webapps\ch11\WEB-INF directory, you can make it a linked file. To do that, right-click the ServletInPlace project, select New File, click the Advanced button, check the "Link to file in the file system" checkbox, and click the Browse button. Browse to the webapps\ch11\WEB-INF directory, and click Open. Then in the New File dialog that appears, enter the name of the file to link to, web.xml, and click OK. This adds web.xml to the project, as shown in the Package Explorer at left in Figure 11-6. You can edit web.xml to support the ServletInPlace servlet, as shown in Example 11-5. Example 11-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>Servlet</servlet-name> <servlet-class>org.cookbook.ch11.ServletClass</servlet-class> </servlet> <servlet> <servlet-name>ServletInPlace</servlet-name> <servlet-class>org.cookbook.ch11.ServletInPlaceClass</servlet-class> </servlet> <servlet-mapping> <servlet-name>Servlet</servlet-name> <url-pattern>/org.cookbook.ch11.ServletClass</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>ServletInPlace</servlet-name> <url-pattern>/org.cookbook.ch11.ServletInPlaceClass</url-pattern> </servlet-mapping> </web-app> Figure 11-6. Linking to a fileAfter making these edits to web.xml, restart Tomcat. We've already built the servlet's code, so navigate to the new servlet's URL, http://localhost:8080/ch11/org.cookbook.ch11.ServletInPlaceClass, as shown in Figure 11-7. Figure 11-7. Running the servlet developed in placeAs you can see, letting Eclipse handle the details of storing compiled files and editing imported files in place makes life much easier than having to change files and copy them to the web container's directories all the time. This is the way to go if you are thinking of doing serious web development with JSP/servlets; the whole development/testing/revision cycle is made so much easier this way.
11.7.4 See AlsoRecipe 11.4 on creating servlets; Recipe 11.5 on installing a servlet in Tomcat; Recipe 11.6 on creating a servlet in place; Chapter 9 in Eclipse (O'Reilly). |
< Day Day Up > |