Latest web development tutorials

Servlet Debugging

Testing / debugging Servlet development of the course is always difficult. Servlet often involves a large number of client / server interaction, but may be difficult to reproduce the error.

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

System.out.println ()

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

  • Because System objects are part of the core Java objects, it can be used anywhere without the need to install any additional classes. This includes Servlet, JSP, RMI, EJB's, Common Beans and classes, as well as stand-alone applications.
  • With different stops at the breakpoint, write to System.out does not interfere with the normal flow of execution of the application, which makes it the timing is crucial when it is particularly valuable.

Here is the syntax to use System.out.println () is:

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

All messages that are generated by the above grammar will be recorded in Web server log files.

The message log

Use the appropriate method of logging to record all debug, warning and error messages, which is a very good idea, it is recommended to use log4J record all messages.

Servlet API also provides a simple way of output, using the log () method, as follows:

// Import necessary java library import *;
import javax.servlet *.;
import javax.servlet.http *.;

public class ContextLog extends HttpServlet {
  public void doGet (HttpServletRequest request, 
      HttpServletResponse response) throws ServletException, {
      String par = request.getParameter ( "par1");
      // Call ServletContext.log two methods ServletContext context = getServletContext ();

      if (par == null || par.equals ( ""))
      // Parameter record by Throwable version context.log ( "No message received:",
          new IllegalStateException ( "Missing parameter"));
          context.log ( "Here is the visitor's message:" + par);
      response.setContentType ( "text / html; charset = UTF-8"); out = response.getWriter ();
      String title = "Context Log";
      String docType = "\ n <DOCTYPE html!>";
      out.println (docType +
        "<Html> \ n" +
        "<Head> <title>" + title + "</ title> </ head> \ n" +
        "<Body bgcolor = \" # f0f0f0 \ "> \ n" +
        "<H1 align = \" center \ ">" + title + "</ h1> \ n" +
        "<H2 align = \" center \ "> Messages sent </ h2> \ n" +
        "</ Body> </ html>");
    } // DoGet

ServletContext it text messages to Servlet container log file. For Tomcat, these logs can be found in the <Tomcat-installation-directory> / logs directory.

These log files are indeed emerging frequency errors or problems are given instructions. Because of this, we recommend using the log () function exception catch clause does not usually happen.

Use JDB debugger

You can use the applet or application debugging jdb command to debug Servlet.

To debug a Servlet, we can debug sun.servlet.http.HttpServer, and then execute it as HttpServer Servlet to respond to HTTP requests the browser side. This debugging applet small program is very similar. And debugging applet is different, the actual program being debugged is sun.applet.AppletViewer.

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

  • Set your debugger's classpath classpath, so that it can find sun.servlet.http.Http-Server and related classes.
  • Set your debugger's classpath classpath, so that it can find your servlet and supporting classes, usually in server_root / servlets and server_root / classes in.

You do not usually want to server_root / servlets in your classpath, because it disables reload servlet. However, this rule contains is very useful for debugging. It allows you to debug in HttpServer custom Servlet loader before Servlet set a breakpoint in a Servlet.

If you have set the correct classpath classpath, you can start debugging sun.servlet.http.HttpServer. Servlet can set breakpoints in the code you want to debug, and then given Servlet using a Web browser (http: // localhost: 8080 / servlet / ServletToDebug) makes a request to HttpServer. You'll see the program execution stops at the breakpoint.

Use comments

Comments in the code will help in various ways for debugging. Notes can be used in many other ways debugging process.

The use of Java Servlet comments and single-line comments (// ...), multi-line comments (/ * ... * /) can be used to temporarily remove some Java code. If the bug disappears, you just look at the commented code and identify the problem.

Client and server-side header information

Sometimes, when a Servlet is not as expected, view the original HTTP request and response is very useful. If you are familiar with HTTP configuration, you can read the request and response and see what information these headers.

Important debugging techniques

Here are some tips Servlet debugging:

  • Please note, server_root / classes will not be reloaded, and the server_root / servlets might.
  • It asks the browser to display the original content of the page it displays. This helps to identify the problem format. It is usually an option in the "View" menu.
  • By forcing a full reload the page to ensure that the browser is not a request before the output buffer. In Netscape Navigator, use Shift-Reload, in Internet Explorer, use Shift-Refresh.
  • Make sure the servlet's init () method accepts a parameter ServletConfig and calls super.init (config).