Jsp Debugging
Testing/debugging a JSP or servlet program is always difficult. JSP and Servlet programs tend to involve a lot of client/server interactions, which can easily lead to errors, and it's often hard to reproduce the error environment.
Next, we will provide some tips and suggestions to help you debug your programs.
* * *
## Using System.out.println()
System.out.println() can conveniently mark whether a piece of code is executed. Of course, we can also print various values. Additionally:
* Since the System object is a core Java object, it can be used anywhere without importing additional classes. This includes Servlets, JSP, RMI, EJBs, Beans, classes, and standalone applications.
* Compared to stopping at a breakpoint, using System.out for output does not significantly affect the application's execution flow. This is particularly useful in applications where timing mechanisms are critical.
The syntax for using System.out.println() is as follows:
System.out.println("Debugging message");
Here is a simple example using System.out.print():
System.out.println
Now, if you run the above example, it will produce the following result:
-4-3-2-1012345
If you are using the Tomcat server, you will 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
This method can be used to output variables and other information to the system log for analysis and to identify the root 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 log any information.
Let's rewrite the above code using the logger API from the JDK:
Logger.info
The output is similar to the previous example, but it provides additional information output to the stdout.log file. Here we used 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=-424-Sep-2013 23:31:31 org.apache.jsp.main_jsp _jspService INFO: counter=2 myCount=-324-Sep-2013 23:31:31 org.apache.jsp.main_jsp _jspService INFO: counter=3 myCount=-224-Sep-2013 23:31:31 org.apache.jsp.main_jsp _jspService INFO: counter=4 myCount=-124-Sep-2013 23:31:31 org.apache.jsp.main_jsp _jspService INFO: counter=5 myCount=024-Sep-2013 23:31:31 org.apache.jsp.main_jsp _jspService INFO: counter=6 myCount=124-Sep-2013 23:31:31 org.apache.jsp.main_jsp _jspService INFO: counter=7 myCount=224-Sep-2013 23:31:31 org.apache.jsp.main_jsp _jspService INFO: counter=8 myCount=324-Sep-2013 23:31:31 org.apache.jsp.main_jsp _jspService INFO: counter=9 myCount=424-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 severe(), warning(), info(), config(), fine(), finer(), and finest() methods. The finest() method is used to log the most detailed information, while the severe() method is used to log the most critical information.
Use the Log4J framework to log messages to different files, categorized based on severity and importance.
* * *
## Debugging Tools
NetBeans is an open-source, tree-structured Java Integrated Development Environment (IDE) that supports the development of standalone Java applications and web applications, as well as JSP debugging.
NetBeans supports the following basic debugging features:
* Breakpoints
* Step tracing
* Watchpoints
For detailed information, please 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.
Typically, we directly debug the sun.servlet.http.HttpServer object to see how the HttpServer executes JSP/Servlets when responding to HTTP requests. This is very similar to debugging applets. The difference is that for applets, you actually debug the sun.applet.AppletViewer.
Most debuggers automatically ignore some details when debugging applets because they know how to debug applets. To shift the debugging focus to JSP, you need to do the following two things:
* Set the debugger's classpath so it can find sun.servlet.http.HttpServer and related classes.
* Set the debugger's classpath so it can find your JSP files and related classes.
After setting the classpath, start debugging sun.servlet.http.HttpServer. You can set breakpoints anywhere in the JSP file, as you like, and then use a browser to send a request to the server. You should see the program stop at the breakpoint.
* * *
## Using Comments
Comments in the program can help with debugging in many ways. Comments can be used in various aspects of debugging.
JSP uses Java comments. If a bug disappears, carefully review the code you just commented out; you can usually find the cause.
* * *
## Client and Server Header Modules
Sometimes, when a JSP is not running as expected, it can be useful to examine the raw HTTP request and response. If you are familiar with the structure of HTTP, you can directly observe the request and response to see what is happening with these header modules.
* * *
## Important Debugging Tips
Here are two more tips for debugging JSP:
* Use the browser to display the raw page content to distinguish whether it is a formatting issue. This option is usually under the View menu.
* Ensure the browser does not capture previous request output when forcing a page reload. If using Netscape Navigator, use Shift-Reload; if using IE, use Shift-Refresh.
YouTip