Testing / debugging Servlet is always a difficult point in the process of development and use. Servlet often involves a large number of client / server interactions, and errors can occur but are difficult to reproduce.
Here are some tips and suggestions to help you debug. Because the System object is part of the core Java object, it can be used anywhere without installing any additional classes. This includes Servlet, JSP, RMI, EJB’s, plain Beans and classes, as well as stand-alone applications. Instead of stopping at a breakpoint, write to the The following is to use the All messages generated through the above syntax will be logged 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 use log4J to record all the messages. Servlet API also provides a simple way to output information, using the These log files do indicate the frequency of new errors or problems. Because of this, it is recommended that exceptions that do not normally occur You can debug Servlet using the jdb command to debug applet or an application. To debug a Servlet, we can debug Most debuggers automatically hide how to debug Set the classpath classpath of your debugger so that it can find Set the classpath of your debugger You don’t usually want If you have set the correct classpath Comments in your code help debug in a variety of ways. Comments can be used in many other ways of debugging the process. The Servlet uses Java comments and single-line comments (/ /…), and multiline comments (/) * …* /) can be used to temporarily remove part ofthe Java code. If the bug disappears, take a closer look at the code you just commented out and find out what the problem is. Sometimes it is useful to look at the original HTTP request and response when a Servlet is not as expected. If you are familiar with the HTTP structure, you can read the request and response to see what these headers are. Here are some tips for Servlet debugging: Attention please, Requires the browser to display the original content of the page it displays. This helps to identify format problems. It is usually an option under the View menu. Ensure that the browser has not cached the output of the previous request byforcing a complete reload of the page. In Netscape Navigator, use Shift-Reload, and in Internet Explorer, use Shift-Refresh. Please confirm servlet’s 2.22.1.
System.out.println()
¶
System.out.println()
is used as a tag to test whether a particular pieceof code is executed. We can also print out the value of the variable. In addition:
System.out
it does not interfere with the normal execution of the application, which makes it particularly valuable when timing is critical.
System.out.println()
syntax:System.out.println("Debugging message");
2.22.2. Message log ¶
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");
// Calling two ServletContext. log methods
ServletContext context = getServletContext( );
if (par == null || par.equals(""))
// Record version through 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
log its text message to the log file of the Servlet container. For Tomcat, these logs can be found in the
<Tomcat-installation-directory>/logs
found it in the catalog.
catch
used in the clause
log()
function. 2.22.3. Using the JDB Debugger ¶
sun.servlet.http.HttpServer
and think of it as
HttpServer
execute Servlet to respond to the HTTP request on the browser side. This is very similar to debugging applet Mini Program. Unlike debugging applet, the actual program being debugged is
sun.applet.AppletViewer
.
applet
the details. Similarly, for servlet, you must do the following for the debugger:
sun.servlet.http.Http-Server
and related classes.
classpath
so that it can find your servlet and supported classes, usually in the
server_root/servlets
and
server_root/classes
.
server_root/servlets
in your
classpath
,because it disables the reload of servlet. 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.
classpath
can start debugging``sun.servlet.http.HttpServer`` . You can set a breakpoint in the Servlet code you want to debug and then make a request to HttpServer using the givenServlet ( http://localhost:8080/servlet/ServletToDebug ) through the Web browser. You will see that the execution of the program stops at the breakpoint. 2.22.4. Working with comments ¶
2.22.5. Client and server header information ¶
2.22.6. Important debugging skills ¶
server_root/classes
will not be overloaded, and
server_root/servlets
it might.
init()
method to accept a
ServletConfig
parameter and call the
super.init(config)
.