JSP debugging


It is always so 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.

The following will give some tips and suggestions to help you debug your program.


Using System.out.println()

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 following is the syntax for using System.out.println():

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 results:

-4
-3
-2
-1
0
1
2
3
4
5

If you are using a Tomcat server, you can log the stdout.log file in the logs directory The following content was found:

counter=1
counter=2
counter=3
counter=4
counter=5
counter=6
counter=7
counter=8
counter=9
counter=10

Using this method, variables and other information can be output to the system log to analyze and find out the deep-seated causes of the problem.


Using the JDB Logger

J2SE log framework can provide logging services for any class running in the JVM. So we can use this framework to record any information.

Let us 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 results are similar to the previous ones, however, it can get additional information output to stdout.log in the file. Here we use the info method in logger. Below we give 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, by using sever(), warning(), info(), config(), fine() ,finer(),finest() method. The finest() method is used to record the best information, and the sever() method is used to record the most serious information.

Use the Log4J framework to log messages in different files, and these messages are classified based on severity and importance.


Debugging tool

NetBeans is a tree structure, 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.


Use JDB Debugger

You can use jdb commands in JSPs and servlets to debug just like debugging ordinary 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.


Using comments

Comments in the program can help in many ways in debugging the program. 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.


Client and Server Header Modules

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.


Important debugging tips

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.