1.7. Compile and Test Some Simple Servlets

OK, so your environment is all set. At least you think it is. It would be nice to confirm that hypothesis. Following are three tests that help verify this.

Test 1: A Servlet That Does Not Use Packages

The first servlet to try is a basic one: no packages, no utility (helper) classes, just simple HTML output. Rather than writing your own test servlet, you can just grab HelloServlet.java (Listing 1.3) from the book’s source code archive at http://www.moreservlets.com. If you get compilation errors, go back and check your CLASSPATH settings (Section 1.6)—you most likely erred in listing the location of the JAR file that contains the servlet classes (e.g., servlet.jar). Once you compile Hello-Servlet.java, put HelloServlet.class in the appropriate location (usually the WEB-INF/ classes directory of your server’s default Web application). Check your server’s documentation for this location, or see the following list for a summary of the locations used by Tomcat, JRun, and ServletExec. Then, access the servlet with the URL http://localhost/servlet/HelloServlet (or http://localhost:8080/servlet/HelloServlet if you chose not to change the port number as described in Section 1.3). You should get something similar to Figure 1-8. If this URL fails but the test of the server itself (Section 1.4) succeeded, you probably put the class file in the wrong directory.

Figure 1-8. Result of HelloServlet.


  • Tomcat Directory.

    install_dir/webapps/ROOT/WEB-INF/classes

  • JRun Directory.

    install_dir/servers/default/default-app/WEB-INF/classes

  • ServletExec Directory.

    install_dir/Servlets

  • Corresponding URL.

    http://host/servlet/HelloServlet

Listing 1.3. HelloServlet.java
import java.io.*; 
import javax.servlet.*; 
import javax.servlet.http.*; 

/** Simple servlet used to test server. */ 

public class HelloServlet extends HttpServlet {
  public void doGet(HttpServletRequest request, 
                    HttpServletResponse response) 
      throws ServletException, IOException {
    response.setContentType("text/html"); 
    PrintWriter out = response.getWriter(); 
    String docType = 
      "<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 " + 
      "Transitional//EN">
"; 
    out.println(docType + 
                "<HTML>
" + 
                "<HEAD><TITLE>Hello</TITLE></HEAD>
" + 
                "<BODY BGCOLOR="#FDF5E6">
" + 
                "<H1>Hello</H1>
" + 
                "</BODY></HTML>"); 
  } 
} 

Test 2: A Servlet That Uses Packages

The second servlet to try is one that uses packages but no utility classes. Again, rather than writing your own test, you can grab HelloServlet2.java (Listing 1.4) from the book’s source code archive at http://www.moreservlets.com. Since this servlet is in the moreservlets package, it should go in the moreservlets directory, both during development and when deployed to the server. If you get compilation errors, go back and check your CLASSPATH settings (Section 1.6)—you most likely forgot to include “.” (the current directory). Once you compile HelloServlet2.java, put HelloServlet2.class in the moreservlets subdirectory of whatever directory the server uses for servlets that are not in custom Web applications (usually the WEB-INF/classes directory of the default Web application). Check your server’s documentation for this location, or see the following list for a summary of the locations for Tomcat, JRun, and ServletExec. For now, you can simply copy the class file from the development directory to the deployment directory, but Section 1.8 (Establish a Simplified Deployment Method) will provide some options for simplifying the process.

Once you have placed the servlet in the proper directory, access it with the URL http://localhost/servlet/moreservlets.HelloServlet2. You should get something similar to Figure 1-9. If this test fails, you probably either typed the URL wrong (e.g., used a slash instead of a dot after the package name) or put HelloServlet2.class in the wrong location (e.g., directly in the server’s WEB-INF/classes directory instead of in the moreservlets subdirectory).

Figure 1-9. Result of HelloServlet2.


  • Tomcat Directory.

    install_dir/webapps/ROOT/WEB-INF/classes/moreservlets

  • JRun Directory.

    install_dir/servers/default/default-app/WEB-INF/classes/moreservlets

  • ServletExec Directory.

    install_dir/Servlets/moreservlets

  • Corresponding URL.

    http://host/servlet/moreservlets.HelloServlet2

Listing 1.4. moreservlets/HelloServlet2.java
							package moreservlets; 

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

/** Simple servlet used to test the use of packages. */ 

public class HelloServlet2 extends HttpServlet {
  public void doGet(HttpServletRequest request, 
                    HttpServletResponse response) 
      throws ServletException, IOException {
    response.setContentType("text/html"); 
    PrintWriter out = response.getWriter(); 
    String docType = 
      "<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 " + 
      "Transitional//EN">
"; 
    out.println(docType + 
                "<HTML>
" + 
                "<HEAD><TITLE>Hello (2)</TITLE></HEAD>
" + 
                "<BODY BGCOLOR="#FDF5E6">
" + 
                "<H1>Hello (2)</H1>
" + 
                "</BODY></HTML>"); 
  } 
} 

1.7.3. A Servlet That Uses Packages and Utilities

The final servlet you should test to verify the configuration of your server and development environment is one that uses both packages and utility classes. Listing 1.5 presents HelloServlet3.java, a servlet that uses the ServletUtilities class (Listing 1.6) to simplify the generation of the DOCTYPE (specifies the HTML version— useful when using HTML validators) and HEAD (specifies the title) portions of the HTML page. Those two parts of the page are useful (technically required, in fact), but are tedious to generate with servlet println statements. Again, the source code can be found at http://www.moreservlets.com.

Since both the servlet and the utility class are in the moreservlets package, they should go in the moreservlets directory. If you get compilation errors, go back and check your CLASSPATH settings (Section 1.6)—you most likely forgot to include the top-level development directory. I’ve said it before, but I’ll say it again: your CLASSPATH must include the top-level directory of your package hierarchy before you can compile a packaged class that makes use of another class from the same package. This requirement is not particular to servlets; it is the way packages work on the Java platform in general. Nevertheless, many servlet developers are unaware of this fact, and it is one of the (perhaps the) most common errors beginning developers encounter.

Core Warning

Your CLASSPATH must include your top-level development directory. Otherwise, you cannot compile servlets that are in packages and that also use classes from the same package.


Once you compile HelloServlet3.java (which will automatically cause ServletUtilities.java to be compiled), put HelloServlet3.class and ServletUtilities.class in the moreservlets subdirectory of whatever directory the server uses for servlets that are not in custom Web applications (usually the WEB-INF/classes directory of the default Web application). Check your server’s documentation for this location, or see the following list for a summary of the locations used by Tomcat, JRun, and ServletExec. Then, access the servlet with the URL http://localhost/servlet/moreservlets.HelloServlet3. You should get something similar to Figure 1-10.

Figure 1-10. Result of HelloServlet3.


  • Tomcat Directory.

    install_dir/webapps/ROOT/WEB-INF/classes/moreservlets

  • JRun Directory.

    install_dir/servers/default/default-app/WEB-INF/classes/moreservlets

  • ServletExec Directory.

    install_dir/Servlets/moreservlets

  • Corresponding URL.

    http://host/servlet/moreservlets.HelloServlet3

Listing 1.5. moreservlets/HelloServlet3.java
							package moreservlets; 

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

/** Simple servlet used to test the use of packages 
 *  and utilities from the same package. 
 */ 

public class HelloServlet3 extends HttpServlet {
  public void doGet(HttpServletRequest request, 
                    HttpServletResponse response) 
      throws ServletException, IOException {
    response.setContentType("text/html"); 
    PrintWriter out = response.getWriter(); 
    String title = "Hello (3)"; 
    out.println(ServletUtilities.headWithTitle(title) + 
                "<BODY BGCOLOR="#FDF5E6">
" + 
                "<H1>" + title + "</H1>
" + 
                "</BODY></HTML>"); 
  } 
} 

Listing 1.6. moreservlets/ServletUtilities.java
							package moreservlets; 

import javax.servlet.*; 
import javax.servlet.http.*; 

/** Some simple time savers. Note that most are static methods. */ 

public class ServletUtilities {
  public static final String DOCTYPE = 
    "<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 " + 
    "Transitional//EN">"; 

  public static String headWithTitle(String title) {
    return(DOCTYPE + "
" + 
           "<HTML>
" + 
           "<HEAD><TITLE>" + title + "</TITLE></HEAD>
"); 
  } 

  ... 
} 

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.189.228