You want to edit web.xml in Eclipse while keeping it in the Tomcat directories.
Link to needed files such as web.xml in your project.
To 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 file
After 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 place
As 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.
| || |
You also can create linked folders in Eclipse. A folder in an Eclipse project linked to the filesystem displays what's in a particular folder in the computer's filesystem. To create a linked folder, right-click a project, and select New Folder. Click the Advanced button in the New Folder dialog, and check the "Link to folder in the file system" checkbox; then click the Browse button, and browse to the filesystem folder you want to display in Eclipse. Click OK, enter the name for this new folder in the Folder Name box, and click OK.
11.7.4 See Also
Recipe 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).