Latest web development tutorials

JSP debugging

To test / debug a JSP or servlet program is always so difficult. JSP and Servlets procedures tend to involve interaction between a large number of client / server, which is likely to generate an error, and it is difficult to reproduce the error environment.

Next will be given some tips and tips to help you debug your program.


Use System.out.println ()

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

  • Since the System object becomes the core Java object that can be used anywhere without the introduction of additional classes. Including the use of Servlets, JSP, RMI, EJB's, Beans, classes and stand-alone applications.
  • Compared with stops at the breakpoint, using System.out output will not have a significant impact on the operation of process applications, this feature is very important in the timing mechanism of the application is very useful.

Next, given the syntax System.out.println () is:

System.out.println("Debugging message");

This is a simple example of using System.out.print () is:

<%@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, then it will produce the following results:

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

If you are using Tomcat server, you will be able to find more of the following in the stdout.log file logs directory under:

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

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


Use JDB Logger

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 code above, use the JDK logger API:

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

It is similar to results of previous, but it can get additional information to the stdout.log file. Here we use the info logger method. Below we give a snapshot 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 using a variety of priority, by using sever (), warning (), info (), config (), fine (), finer (), finest () method. finest () method is used to record the best information, and sever () method is used to record the most serious information.

Use Log4J framework logs messages in a different file, these messages based on severity and importance to classify.


Debugging Tools

NetBeans is a tree structure, open source Java integrated development environment, support the development of stand-alone Java applications and network applications, but also support JSP debugging.

NetBeans supports the following a few basic debugging features:

  • Breakpoints
  • Single-step tracking
  • Observation Point

Detailed information can be viewed NetBeans manual.


Use JDB Debugger

You can use the jdb command in JSP and servlets in for debugging, just like debug as a normal application.

Usually, we direct debugging sun.servlet.http.HttpServer object to view the situation HttpServer implementation of JSP / Servlets in response to HTTP requests. This is very similar to debugging applets. The difference is that, applets actual debugging program is sun.applet.AppletViewer.

Most of the debugger when debugging applets can automatically ignore some of the details, because it knows how to debug applets. If you want to debug JSP objects transferred to the body, you need to do two things:

  • Setting the debugger classpath, it can be found sun.servlet.http.Http-Server and related classes.
  • Setting the debugger classpath, to enable it to find your JSP files and related classes.

Set classpath, it began commissioning sun.servlet.http.Http-Server. You can set breakpoints in the JSP file anywhere, as long as you like, and then use the browser sends a request to the server should be able to see the program stopped at a breakpoint.


Use comments

Program annotations to the debugger play a helpful role in many aspects. Comments can be used in many ways in the debugger.

JSP uses Java annotations. If a BUG disappeared, please carefully review the comments you have just had the code can usually find out why.


The head of the client and server modules

Sometimes, when the JSP is not running in a predetermined manner when viewing the raw HTTP requests and responses are also useful. If the structure is very familiar with HTTP, you can directly observe the request and response headers and seeing how the modules in the end.


Important debugging techniques

Here we are again revealed two JSP debugging tips:

  • Using the browser displays the original content of the page, is used to distinguish whether the format. This option is usually in the View menu.
  • Ensure that the browser when the forced reload the page does not capture the output of the previous request. If you are using the Netscape Navigator browser, use Shift-Reload; if using IE browser, then use Shift-Refresh.