Servlet debugging
Testing/debugging Servlets is always a difficult point in the development and use process. Servlets often involve a lot of client/server interaction, and errors can occur but are difficult to reproduce.
Here are some tips and suggestions to help you debug.
System.out.println()
System.out.println() is used as a marker to test whether a specific piece of code is executed. We can also print out the value of a variable. Additionally:
Since the System object is part of the core Java objects, it can be used anywhere without the need to install any additional classes. This includes Servlets, JSPs, RMI, EJB's, plain Beans and classes, as well as standalone applications.
Unlike stopping at a breakpoint, writing to System.out does not interfere with the normal execution flow of the application, which makes it especially useful when timing is critical. valuable.
The following is the syntax for using System.out.println():
System.out.println("Debugging message");
All messages generated by the above syntax will be logged in the web server log file.
Message log
It is a very good idea to use appropriate logging methods to log all debug, warning and error messages. It is recommended to use log4J to log all information.
Servlet API also provides a simple way to output information, using the log() method, as shown below:
// 导入必需的 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 message to the log file of the Servlet container . For Tomcat, these logs can be found in the <Tomcat-installation-directory>/logs directory.
These log files do give an indication of the frequency of new errors or problems. Because of this, it is recommended to use the log() function in the catch clause of exceptions that would not normally occur.
Using the JDB Debugger
You can debug a Servlet using the debug applet or the application's jdb command.
In order to debug a Servlet, we can debug sun.servlet.http.HttpServer, and then regard it as HttpServer executing the Servlet to respond to the HTTP request on the browser side. This is very similar to debugging an applet. Unlike debugging applets, the actual program being debugged is sun.applet.AppletViewer.
Most debuggers automatically hide the details of how to debug an applet. Likewise, for servlets, you must do the following for the debugger:
Set your debugger's 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 servlets and supporting classes, typically in server_root/servlets and server_root/classes.
You generally don't want server_root/servlets on your classpath as it disables reloading of servlets. But this inclusion rule is very useful for debugging. It allows your debugger to set breakpoints in the servlet before it is loaded by the custom servlet loader in HttpServer.
If you have set the correct classpath classpath, you can start debugging sun.servlet.http.HttpServer. You can set breakpoints in the Servlet code you want to debug and then make a request to HttpServer through a web browser using the given Servlet (http://localhost:8080/servlet/ServletToDebug). You will see that the program execution stops at the breakpoint.
Using Comments
Comments in your code help with debugging in various ways. Comments can be used in many other ways to debug the process.
This Servlet uses Java comments and single-line comments (//...). Multi-line comments (/* ...*/) can be used to temporarily remove part of the Java code. If the bug goes away, take a closer look at the code you just commented and figure out where the problem lies.
Client-side and server-side header information
Sometimes, when a servlet does not behave as expected, it is very useful to view the original HTTP request and response. If you're familiar with HTTP structure, you can read the request and response to see what these headers are.
Important Debugging Tips
Listed below are some Servlet debugging tips:
- ##Please note that server_root/classes will not be overloaded, but server_root/servlets might.
- Requires the browser to display the original content of the page it displays. This helps identify formatting issues. It's usually an option under the View menu.
- Ensures that the browser has not cached the output of the previous request by forcing a complete reload of the page. In Netscape Navigator, use Shift-Reload, and in Internet Explorer, use Shift-Refresh.
- Please confirm that the servlet's init() method accepts a ServletConfig parameter and calls super.init(config).