Easy Tutorial
❮ Jstl Sql Query Tag Jstl Sql Setdatasource Tag ❯

JSP Debugging

Testing and debugging a JSP or servlet program can always be challenging. JSP and servlets tend to involve a lot of client/server interactions, which can lead to errors and make it difficult to reproduce the error environment.

The following are 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:

Here is the syntax for using System.out.println():

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

This 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 the Tomcat server, you will find the following additional content 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

This method allows you to output variables and other information to the system log for analysis and to find the underlying cause of the problem.


Using JDB Logger

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

Let's rewrite the above code using the JDK's 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>

Its output is similar to the previous example, but it provides additional information in the stdout.log file. Here, we used the info method of the logger. Below is 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

❮ Jstl Sql Query Tag Jstl Sql Setdatasource Tag ❯