English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
To test/It is always difficult to debug a JSP or servlet program. JSP and servlet programs tend to involve a large amount of client-side/Interaction between servers may very likely produce errors and it is difficult to reproduce the error environment.
The following will provide some tips and suggestions to help you debug your program.
System.out.println() can conveniently mark whether a segment of code is executed. Of course, we can also print out various values. In addition:
Since the System object has become a core object in Java, it can be used anywhere without introducing an additional class. The scope of use includes Servlets, JSP, RMI, EJB's, Beans, classes, and standalone applications.
Compared to stopping the application at a breakpoint, using System.out for output does not have a significant impact on the application's execution flow, which is very useful in applications where timing mechanisms are very important.
Next, the syntax for using System.out.println() is given:
System.out.println("Debugging message");
This is a simple example using System.out.print():
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <html> <head><title>System.out.println</title></head> <body> <c:forEach var="counter" begin="1" end="10" step="1"> <c:out value="${counter-5"}/></br> <% System.out.println( "counter= " + pageContext.findAttribute("counter") ); %> </c:forEach> </body> </html>
Now, if you run the above instance, it will produce the following result:
-4 -3 -2 -1 0 1 2 3 4 5
If you are using the Tomcat server, you can find the following additional content in the stdout.log file under the logs directory:
counter=1 counter=2 counter=3 counter=4 counter=5 counter=6 counter=7 counter=8 counter=9 counter=10
Using this method, you can output variables and other information to the system log, which is used to analyze and find the underlying causes of the problem.
J2The SE logging framework can provide logging services for any class running in the JVM. Therefore, we can use this framework to log any information.
Let's rewrite the above code using the logger API from JDK:
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@page import="java.util.logging.Logger" %> <html> <head><title>Logger.info</title></head> <body> <% Logger logger=Logger.getLogger(this.getClass().getName());%> <c:forEach var="counter" begin="1" end="10" step="1"> <c:set var="myCount" value="${counter-5"} /> <c:out value="${myCount}"/></br> <% String message = "counter=" + pageContext.findAttribute("counter") + "myCount=" + pageContext.findAttribute("myCount"); logger.info(message); %> </c:forEach> </body> </html>
Its runtime results are similar to the previous ones, but it can obtain additional information output to the stdout.log file. In this case, we used the logger's info method. Below is a snapshot from the stdout.log file:
24-Sep-2013 23:31:31 org.apache.jsp.main_jsp _jspService INFO: counter=1 myCount=-4 24-Sep-2013 23:31:31 org.apache.jsp.main_jsp _jspService INFO: counter=2 myCount=-3 24-Sep-2013 23:31:31 org.apache.jsp.main_jsp _jspService INFO: counter=3 myCount=-2 24-Sep-2013 23:31:31 org.apache.jsp.main_jsp _jspService INFO: counter=4 myCount=-1 24-Sep-2013 23:31:31 org.apache.jsp.main_jsp _jspService INFO: counter=5 myCount=0 24-Sep-2013 23:31:31 org.apache.jsp.main_jsp _jspService INFO: counter=6 myCount=1 24-Sep-2013 23:31:31 org.apache.jsp.main_jsp _jspService INFO: counter=7 myCount=2 24-Sep-2013 23:31:31 org.apache.jsp.main_jsp _jspService INFO: counter=8 myCount=3 24-Sep-2013 23:31:31 org.apache.jsp.main_jsp _jspService INFO: counter=9 myCount=4 24-Sep-2013 23:31:31 org.apache.jsp.main_jsp _jspService INFO: counter=10 myCount=5
Messages can be sent with various priorities using the sever(), warning(), info(), config(), fine(), finer(), finest() methods. The finest() method is used to log the best information, while the sever() method is used to log the most severe information.
Use Log4Use the J framework to log messages to different files, which are categorized based on severity and importance.
NetBeans is a tree-structured, open-source Java integrated development environment that supports the development of independent Java applications and network applications, as well as JSP debugging.
NetBeans supports the following basic debugging features:
Breakpoints
Step-by-step Tracking
Breakpoints
For more detailed information, please refer to the NetBeans User Manual.
You can use the jdb command to debug JSP and servlets, just like debugging a regular application.
Generally, we directly debug the sun.servlet.http.HttpServer object to view how the HttpServer executes JSP in response to HTTP requests./The situation with Servlets is very similar to debugging applets. The difference lies in the fact that the actual debugging of applets is done on sun.applet.AppletViewer.
Most debuggers can automatically ignore some details when debugging applets, as they know how to debug applets. If you want to shift the debugging focus to JSP, you need to prepare the following two points:
Set the classpath of the debugger to let it find sun.servlet.http.Http-Server and related classes.
Set the classpath of the debugger to let it find your JSP file and related classes.
After setting up the classpath, start debugging sun.servlet.http.Http-Server. You can set breakpoints anywhere in the JSP file as you like, and then send a request to the server using the browser to see the program stop at the breakpoint.
Comments in the program can help with debugging in many ways. Comments can be used in many aspects of debugging the program.
JSP uses Java comments. If a BUG disappears, please carefully check the code you just commented on, as it is usually the cause.
Sometimes, when JSP does not run as expected, it is also useful to view the raw HTTP request and response. If you are familiar with the structure of HTTP, you can directly observe the request and response and see what is wrong with these header modules.
Here are two more debugging tips for JSP:
Use the browser to display the original page content to distinguish whether it is a formatting issue. This option is usually under the View menu.
Ensure that the browser does not capture the previous request output when it is forced to reload the page. If you are using the Netscape Navigator browser, use Shift-Reload; if you are using the IE browser, use Shift-Refresh.