DekGenius.com
Previous Section  < Day Day Up >  Next Section

Recipe 11.6 Creating a Servlet in Place

11.6.1 Problem

You want to develop web applications in place, without having to copy files over from the Eclipse directories.

11.6.2 Solution

Set the project's output folder to the web container's proper target directories.

11.6.3 Discussion

As an example, we'll create a new project here, ServletInPlace, and store the needed Tomcat files in Tomcat directories. Create this project by bringing up the New Project folder and entering the name ServletInPlace. To specify that we want the compiled output to go into the Tomcat directories rather than be stored locally, click Next, and click the Browse button next to the Default output folder box to open the Folder Selection dialog.

Next, click the Create new folder button; then click the Advanced button in the New Folder dialog. To connect to the Tomcat directory we want to use, check the "Link to folder in the file system" checkbox and click the Browse button. Browse to the target directory for our compiled code, jakarta-tomcat-5.0.19\webapps\Ch11\WEB-INF\classes, and click OK, bringing up the New Folder dialog again. In that folder, give this new folder the name output, as shown in Figure 11-5, and click OK.

Figure 11-5. Setting up a new output folder
figs/ecb_1105.gif


When we build this project, all compiled output will be stored where we want it, in the classes folder (actually, in this case, in the classes\org\cookbook\ch11 folder, following the package name we're using). Create the class ServletInPlaceClass, and enter the code you see in Example 11-4.

Example 11-4. An in-place servlet
package org.cookbook.ch11;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class ch11_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>");
    }
}

As we've done before, add servlet-api.jar to the build path. Now when you build this project, ServletInPlace.class is stored automatically in the Tomcat webapps\WEB-INF\classes\org\cookbook\ch11 directory, as it should be.

11.6.4 See Also

Recipe 11.7 on editing web.xml in place; Chapter 9 in Eclipse (O'Reilly).

    Previous Section  < Day Day Up >  Next Section