Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Servlet debugging


May 15, 2021 Servlet


Table of contents


Servlet debugging

Testing/debugging servlets is always a difficult part of the development process. Servlets often involve a large number of client/server interactions and can be error-like but difficult to reproduce.

Here are some tips and suggestions to help you debug.

System.out.println()

System.out.println() is used as a tag to test whether a particular piece of code is executed. W e can also print out the value of the variable. In addition:

  • Because the System object is part of the core Java object, it can be used anywhere without the need to install any additional classes. T his includes servlets, JSPs, RMI, EJB's, common Beans, and classes, as well as stand-alone applications.
  • Unlike stopping at break points, writing to System.out does not interfere with the application's normal execution process, which makes it particularly valuable when timing is critical.

Here's the syntax for using System.out.println():

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

All messages generated through the syntax above are logged in the Web server log file.

The message log

It is a good idea to use the appropriate logging method to log all debugs, warnings, and error messages, and it is recommended to use log4J to log all messages.

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

// 导入必需的 java 库
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");
      // 调用两个 ServletContext.log 方法
      ServletContext context = getServletContext( );

      if (par == null || par.equals(""))
      // 通过 Throwable 参数记录版本
      context.log("No message received:",
          new IllegalStateException("Missing parameter"));
      else
          context.log("Here is the visitor's message: " + par);
      
      response.setContentType("text/html");
      java.io.PrintWriter out = response.getWriter( );
      String title = "Context Log";
      String docType =
      "<!doctype html public \"-//w3c//dtd html 4.0 " +       "transitional//en\">\n";
      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 records its text messages in the log file of the servlet container. For Tomcat, these logs can be found in the directory of the .lt;Tomcat-design-directory./logs.

These log files do indicate the frequency of emerging errors or problems. Because of this, it is recommended to use the log() function in catch clauses for exceptions that do not normally occur.

Use the JDB debugger

You can debug the servlet using the jdb command of the debug applet or application.

In order to debug a servlet, we can debug sun.servlet.httpserver and think of it as an Httpserver executing a servlet in response to an HTTP request on the browser side. T his is very similar to debugging applet applet apple programs. Unlike debugging applets, the program that is actually debugged is sun.applet.AppletViewer.

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

  • Set the class path classpath for your debugger so that it can find sun.servlet.http-Server and related classes.
  • Set the class path classpath for your debugger so that it can find your servlet and supported classes, usually in server_root/servlets and server_root/classes.

You don't server_root/servlets in your classpath because it disables the reloading of the servlet. H owever, this inclusion rule is useful for debugging. It allows your debugger to set break points in the servlet before the custom servlet loader in the Http Server loads the servlet.

If you've set up the correct class path classpath, you can start debugging sun.servlet.http://www.httpserver. Y ou can set break points in the servlet code that you want to debug, and then make a request to httpserver using a given servlet (http://localhost:8080/servlet/ServletToDebug) through a web browser. You'll see that the program executes to a break point and stops.

Use comments

Comments in your code help you debug in a variety of ways. Comments can be used in many other ways for debugging procedures.

The servlet uses Java comments and single-line comments (//... ) , a multi-line comment ( / . If the bug disappears, take a closer look at the code you just commented on and find out what the problem is.

Client and server header information

Sometimes, it is useful to view the original HTTP request and response when a servlet is not what you expect. If you're familiar with http structures, you can read requests and responses to see what the header information is.

Important debugging techniques

Here are some tips for servlet debugging:

  • Note that server_root/classes are not overloaded, server_root/servlets may.
  • Ask the browser to display the original content of the page it displays. T his helps identify formatting issues. It is usually an option under the View menu.
  • Make sure that the browser does not cache the output of the previous request by enforcing a full reload of the page. In Netscape Navigator, use Shift-Reload, and in Internet Explorer, use Shift-Refresh.
  • Make sure that the init() method of the servlet accepts a servletConfig parameter and calls super.init (config).