English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Testing/Debugging Servlets has always been a difficult point in the development process./Server interaction may result in errors that are difficult to reproduce.
Here are some tips and suggestions to help you debug.
System.out.println() is used as a marker to test whether a specific piece of code is executed. We can also print the value of variables. In addition:
Since the System object is part of the core Java object, it can be used anywhere without the need to install any additional classes. This includes Servlets, JSPs, RMI, EJB's, ordinary 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, making it particularly valuable when timing is crucial.
The following is the syntax for using System.out.println():
System.out.println("Debugging message");
All messages generated by the above syntax will be recorded in the Web server log file.
It is a very good idea to use appropriate logging methods to record all debug, warning, and error messages, and it is recommended to do so. log4J to record all messages.
The Servlet API also provides a simple way to output information, using the log() method, as follows:
// 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 the version through the 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 = "<!DOCTYPE html> \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 logs its text messages to the Servlet container's log file. For Tomcat, these logs can be found in <Tomcat-installation-directory>/found in the logs directory.
These log files indeed indicate the frequency of new errors or issues. Therefore, it is recommended to use the log() function in the catch clause for exceptions that usually do not occur.
You can use the jdb command to debug Servlets, which is used to debug applets or applications.
To debug a Servlet, we can debug sun.servlet.http.HttpServer and then view it as HttpServer executing Servlet to respond to HTTP requests from the browser side. This is very similar to debugging applets. Unlike debugging applets, the actual program being debugged is sun.applet.AppletViewer.
Most debuggers will automatically hide the details of how to debug applets. Similarly, for servlets, you must help the debugger perform the following operations:
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 servlets and supported classes, usually in server_root/servlets and server_root/classes.
You usually would not want server_root/servlets in your classpath because it will disable servlet reloading. However, 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 classpath, you can start debugging sun.servlet.http.HttpServer. You can set breakpoints in the Servlet code you want to debug and then use the given Servlet (http://localhost:8080/servlet/ServletToDebug sends a request to HttpServer. You will see that the program stops at the breakpoint.
Comments in the code help in various ways to debug. 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 part of the Java code. If the bug disappears, carefully look at the code you just commented out and find the problem.
Sometimes, when a Servlet is not working as expected, it is very useful to view the original HTTP request and response. If you are familiar with HTTP structure, you can read the request and response to see what these header information actually is.
The following lists some tips for Servlet debugging:
Please note, server_root/classes will not reload, while server_root/servlets may.
Request the browser to display the original content of the page it is displaying. This helps identify formatting issues. It is usually an option under the 'View' menu.
Ensure that the browser has not cached the output of the previous request by forcing a complete reload of the page. In Netscape Navigator, please use Shift-Reload, in Internet Explorer, please use Shift-Refresh.
Please confirm that the servlet's init() method accepts a ServletConfig parameter and calls super.init(config).