WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

JSP debugging


It is always so difficult to test / debug a JSP or servlet program. JSP and Servlets programs tend to involve a large number of client / server interactions, which are likely to cause errors and make it difficult to reproduce the environment in which they occurred.

Here are some tips and tricks to help you debug your program.


Use System.out.println ()

System.out.println () can easily mark whether a piece of code is executed. Of course, we can also print out various values. In addition:

  • Since the System object becomes the core Java object, it can be used anywhere without introducing additional classes. The scope of use includes Servlets, JSP, RMI, EJB's, Beans, classes and stand-alone applications.
  • Compared with stopping at a breakpoint, using System.out for output does not have a significant impact on the running process of the application. This feature is very useful in applications where the timing mechanism is 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 result:

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

If you are using a Tomcat server, you can find the following 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, variables and other information can be output to the system log, which can be used to analyze and find the underlying cause of the problem.


Use JDB Logger

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>

Its results are similar to the previous ones, however, it can get additional information output to stdout.log file. Here we use the info method in the 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 the sever (), warning (), info (), config (), fine (), finder (), and find () methods. 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, which are categorized based on severity and importance.


Debugging tools

NetBeans is a tree structure, is an open source Java comprehensive development environment, 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 JSP and servlets to debug, 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 the applets program actually debugs the sun.applet.AppletViewer.

Most debuggers automatically ignore some details when debugging applets because it knows how to debug applets. If you want to transfer debugging objects to JSP, you need to do the following two things:

  • Set the classpath of the debugger so that it can find sun.servlet.http.Http-Server  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.Http-Server. You can set breakpoints anywhere in the JSP file, and if you like, then use your browser to send a request to the server and you should see the program stop at the breakpoint.


Use comments

The comments in the program help the debugging of the program in many ways. Comments can be used in many aspects of debugging programs.

JSP uses Java annotations. If a bug disappears, take a closer look at the code you just commented on, and usually find out why.


Client and server header modules

Sometimes it is useful to view raw HTTP requests and responses when the JSP is not running as intended. If you are familiar with the structure of HTTP, you can directly observe the request and response and see what happened to 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 it is a formatting issue. This option is usually under the View menu.
  • Make sure that the browser did not capture the previous request output when forcibly reloading the page. If using Netscape Navigator, use Shift-Reload; if using IE, use Shift-Refresh.