Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

JSP Debugging

JSP Debugging

Testing/debugging a JSP or servlet program is always difficult. JSP and Servlet programs tend to involve a large amount of client/server interaction, which is likely to produce errors and makes it hard to reproduce the conditions in which they occur.

Some tips and suggestions will be given below 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 all kinds of values. In addition:

The syntax for using System.out.println() is given below:



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" %>



System.out.println





   
<% System.out.println( "counter= " + pageContext.findAttribute("counter") ); %>

Now, if you run the example above, it will produce the following result:



-4

-3

-2

-1

0

1

2

3

4

5

If you are using the Tomcat server, you will find the following added to 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 the underlying causes of problems.


Using the JDB Logger

The J2SE logging framework provides logging services for any class running in the JVM. Therefore we can use this framework to log any information.

Let's rewrite the code above using the logger API in the JDK:



<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<%@page import="java.util.logging.Logger" %>





Logger.info



<% Logger logger=Logger.getLogger(this.getClass().getName());%>





   

   
<% String message = "counter=" + pageContext.findAttribute("counter") + " myCount=" + pageContext.findAttribute("myCount"); logger.info( message ); %>

Its result is similar to the previous one, but it can obtain additional information output 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 at various priority levels using the sever(), warning(), info(), config(), fine(), finer(), and finest() methods. The finest() method logs the finest information, while the sever() method logs the most severe information.

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


Debugging tools

NetBeans is a tree-structured, open-source integrated Java development environment that supports developing standalone Java applications and web applications, as well as JSP debugging.

NetBeans supports the following basic debugging features:

For detailed information, see the NetBeans user manual.


Using the JDB Debugger

You can use the jdb command to debug JSP and servlets, just as you would debug an ordinary application.

Usually, we debug the sun.servlet.http.HttpServer object directly to see how HttpServer executes JSP/Servlets when responding to HTTP requests. This is very similar to debugging applets. The difference is that when debugging applets, what you actually debug is the sun.applet.AppletViewer.

Most debuggers can automatically ignore certain details when debugging applets because they know how to debug applets. If you want to shift the debugging target to JSP, you need to do the following two things:

After setting the classpath, start debugging sun.servlet.http.Http-Server. You can set a breakpoint anywhere in the JSP file you like, 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 with debugging in many ways. Comments can be used in many aspects of debugging a program.

JSP uses Java comments. If a bug disappears, carefully examine the code you just commented out; usually you can find the cause.


Client and Server Headers

Sometimes, when a JSP does not run as expected, it is useful to look at the raw HTTP requests and responses. If you are familiar with the structure of HTTP, you can directly inspect the request and response to see what is wrong with these headers.


Important Debugging Tips

Here we reveal two more tips for debugging JSP: