Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Servlet Debugging

Servlet Debugging

Testing/debugging Servlets is always a difficult part of the development process. Servlets often involve a large amount of client/server interaction, which can produce errors that are hard to reproduce.

Here are some tips and suggestions that can help you debug.

System.out.println()

System.out.println() is used as a marker to test whether a particular piece of code is executed. We can also print out the value of variables. In addition:

The following is the syntax for using System.out.println():



System.out.println("Debugging message");

All messages generated by the syntax above will be recorded in the Web server's log file.

Message Logging

It is a very good idea to use appropriate logging methods to record all debugging, warning, and error messages; it is recommended to use log4J to record all messages.

The Servlet API also provides a simple way to output messages, using the log() method, as shown below:



// Import necessary Java libraries

import java.io.*;

import javax.servlet.*;

import javax.servlet.http.*;



public class ContextLog extends HttpServlet {

  public void doGet(HttpServletRequest request, 

      HttpServletResponse response) throws ServletException,

         java.io.IOException {

    

      String par = request.getParameter("par1");

      // Call two ServletContext.log methods

      ServletContext context = getServletContext( );



      if (par == null || par.equals(""))

      // Record version using Throwable parameter

      context.log("No message received:",

          new IllegalStateException("Missing parameter"));

      else

          context.log("Here is the visitor's message: " + par);

      

      response.setContentType("text/html;charset=UTF-8");

      java.io.PrintWriter out = response.getWriter( );

      String title = "Context Log";

      String docType = " \n";

      out.println(docType +

        "\n" +

        "" + title + "\n" +

        "\n" +

        "

" + title + "

\n" + "

Messages sent

\n" + ""); } //doGet }

ServletContext logs its text messages to the Servlet container's log file. For Tomcat, these logs can be found in the /logs directory.

These log files do give an indication of the frequency of newly occurring errors or problems. Because of this, it is recommended to use the log() function in catch clauses of exceptions that normally would not occur.

Using the JDB Debugger

You can use the jdb command used to debug applets or applications to debug Servlets.

To debug a Servlet, we can debug sun.servlet.http.HttpServer and then treat it as the HttpServer executing the Servlet to respond to HTTP requests from the browser. This is very similar to debugging an applet. Unlike debugging an applet, the program actually being debugged is sun.applet.AppletViewer.

Most debuggers automatically hide the details of how to debug applets. Similarly, for servlet, you must help the debugger do the following:

You usually would not want server_root/servlets in your classpath because it disables servlet reloading. But this inclusion rule is very useful for debugging. It allows your debugger to set breakpoints in the Servlet before the custom Servlet loader in HttpServer loads the Servlet.

If you have set the correct classpath, you can begin debugging sun.servlet.http.HttpServer. You can set breakpoints in the Servlet code you want to debug, then use the given Servlet (http://localhost:8080/servlet/ServletToDebug) through a Web browser to make a request to HttpServer. You will see the program stop when it reaches the breakpoint.

Using Comments

Comments in code help with debugging in various ways. Comments can be used in many other ways in the debugging process.

This Servlet uses Java comments and single-line comments (//...); multi-line comments (/* ...*/) can be used to temporarily remove parts of the Java code. If the bug disappears, take a careful look at the code you just commented out and find the problem.

Client and Server-Side Header Information

Sometimes, when a Servlet does not behave as expected, it is very useful to view the raw HTTP requests and responses. If you are familiar with the HTTP structure, you can read the requests and responses to see exactly what these headers are.

Important Debugging Tips

The following lists some tips for debugging Servlets: