JSP Debugging
Testing/debugging a JSP or servlet program is always so difficult. 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, some tips and suggestions will be given to help you debug your programs.
Using System.out.println()
System.out.println() can conveniently mark whether a piece of code has been executed. Of course, we can also print out various values. In addition:
- Since the System object became a core Java object, it can be used anywhere without importing additional classes. Its scope of use includes Servlets, JSP, RMI, EJB's, Beans, classes, and standalone applications.
- Compared with stopping at breakpoints, outputting with System.out will not significantly affect the running flow of the application. This feature 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 of 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 result:
-4 -3 -2 -1 0 1 2 3 4 5
If you are using a Tomcat server, you can find the following additional content in the stdout.log file in 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 to analyze and find out the underlying cause of the problem.
Using JDB Logger
The J2SE logging framework can provide logging services for any class running in the JVM. Therefore, we can use this framework to record any information.
Let's rewrite the above code using 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>
Its running result is similar to the previous one, but it can output additional information to the stdout.log file. Here we use the info method of the logger. Below is a snapshot of 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 priority levels by using the sever(), warning(), info(), config(), fine(), finer(), and finest() methods. The finest() method is used to record the finest information, while the sever() method is used to record the most severe information.
Use the Log4J framework to record messages in different files, and these messages are classified based on severity and importance.
Debugging Tools
NetBeans is a tree-structured, open-source Java integrated development environment that supports the development of standalone Java applications and web applications, and also supports JSP debugging.
NetBeans supports the following basic debugging functions:
- Breakpoints
- Single-step tracing
- Watchpoints
For more information, refer to the NetBeans User Manual.
Using JDB Debugger
You can use the jdb command to debug JSP and servlets, just like debugging ordinary applications.
Usually, we directly debug the sun.servlet.http.HttpServer object to see how HttpServer executes JSP/Servlets when responding to HTTP requests. This is very similar to debugging applets. The difference is that applet programs actually debug 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 target 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 breakpoints anywhere in the JSP file, as you like, and then use a browser to send a request to the server, and you should see the program stop at the breakpoint.
Using Comments
Comments in a program help debug the program in many ways. Comments can be used in many aspects of debugging a program.
JSP uses Java comments. If a bug disappears, carefully review the code you just commented out, and you can usually find the cause.
Client and Server Headers
Sometimes, when a 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 going on with these headers.
Important Debugging Tips
Here we reveal two more small tips for debugging JSP:
- Use the browser to display the raw page content to distinguish whether it is a formatting problem. This option is usually under the View menu.
- Make sure that the browser does not capture the previous request output when it is forced to reload the page. If you are using Netscape Navigator, use Shift-Reload; if you are using IE, use Shift-Refresh.