It is always difficult to test/debug a JSP or servlet program. JSP and Servlets programs tend to involve a lot of client/server interaction, which is very likely to cause errors, and it is difficult to reproduce the error environment.
Next, we will give some tips and suggestions to help you debug your program.
System.out.println() can easily mark whether a piece of code has been executed. Of course, we can also print out various values. also:
Since the System object became a core Java object, it can be used anywhere without introducing additional classes. Usage scope includes Servlets, JSP, RMI, EJB's, Beans, classes and stand-alone applications.
Compared with stopping running at a breakpoint, using System.out to output will not have a significant impact on the running process of the application. This feature is very useful in applications where the timing mechanism is very important.
The syntax for using System.out.println() is given next:
System.out.println("Debugging message");Here 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 example, it will produce the following results:
-4-3-2-1012345If you are using a Tomcat server, you will be able to find the following additional content in the stdout.log file in the logs directory:
counter=1counter=2counter=3counter=4counter=5counter=6counter=7counter=8counter=9counter=10Using this method, variables and other information can be output to the system log for analysis and finding the underlying cause of the problem.
The J2SE logging framework provides logging services for any class running in the JVM. So we can use this framework to record any information.
Let's rewrite the above code to use the logger API in the 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>The results are similar to the previous one, however, it can output additional information to the stdout.log file. Here we use the info method in logger. Below we give a snapshot of the stdout.log file:
24-Sep-2013 23:31:31 org.apache.jsp.main_jsp _jspServiceINFO: counter=1 myCount=-424-Sep-2013 23:31:31 org.apache.jsp.main_jsp _jspServiceINFO: counter=2 myCount=- 324-Sep-2013 23:31:31 org.apache.jsp.main_jsp _jspServiceINFO: counter=3 myCount=-224-Sep-2013 23:31:31 org.apache.jsp.main_jsp _jspServiceINFO: counter=4 myCount=-124-Sep-2013 23:31:31 org.apache.jsp.main_jsp _jspServiceINFO: counter=5 myCount=024-Sep-2013 23:31:31 org.apache.jsp.main_jsp _jspServiceINFO: counter=6 myCount=124-Sep-2013 23:31:31 org.apache.jsp.main_jsp _jspServiceINFO: counter= 7myCount=224-Sep-2013 23:31:31 org.apache.jsp.main_jsp _jspServiceINFO: counter=8 myCount=324-Sep-2013 23:31:31 org.apache.jsp.main_jsp _jspServiceINFO: counter=9 myCount=424-Sep-2013 23: 31:31 org.apache.jsp.main_jsp _jspServiceINFO: counter=10 myCount=5Messages can be sent with various priorities, using the sever(), warning(), info(), config(), fine(), finer(), and finest() methods. The finest() method is used to record the best information, and the sever() method is used to record the most severe information.
Use the Log4J framework to log messages in different files, which are classified based on severity and importance.
NetBeans is a tree structure and an open source Java comprehensive development environment that supports the development of independent Java applications and network applications, and also supports JSP debugging.
NetBeans supports the following basic debugging functions:
breakpoint
Single step tracking
observation point
Detailed information can be found in the NetBeans manual.
You can use jdb commands in JSPs and servlets to debug just like normal applications.
Usually, we debug the sun.servlet.http.HttpServer object directly to see how HttpServer executes JSP/Servlets in response to HTTP requests. This is very similar to debugging applets. The difference is that what the applet program actually debugs is sun.applet.AppletViewer.
Most debuggers can automatically ignore some details when debugging applets because they know how to debug applets. If you want to transfer the debugging object to JSP, you need to do the following two things:
Set the debugger's classpath so that it can find sun.servlet.http.Http-Server and related classes.
Set the debugger's classpath so that it can find your JSP files and related classes.
After setting the classpath, start debugging sun.servlet.http.Http-Server. You can set a breakpoint anywhere in the JSP file as you like, and then use the browser to send a request to the server and you should see the program stop at the breakpoint.
Comments in the program play a certain role in debugging the program in many ways. Comments can be used in many aspects of debugging a program.
JSP uses Java annotations. If a bug disappears, take a closer look at the code you just commented, and you can usually find out why.
Sometimes it is also useful to view the raw HTTP requests and responses when the JSP is not behaving as intended. If you are familiar with the structure of HTTP, you can directly observe the request and response and see what is happening with these header modules.
Here we reveal two more tips for debugging JSP:
Use a browser to display the original page content to distinguish whether there is a format problem. This option is usually under the View menu.
Make sure the browser does not capture previous request output when force reloading the page. If you are using Netscape Navigator browser, use Shift-Reload; if you are using IE browser, use Shift-Refresh.