How To Debug JSP Page?

Debugging enables a developer to detect and identifies the errors in the application. But testing a JSP/Servlet compared to other programs is often found to be difficult. Following are some of the suggestions which can be used to test you JSP programs:

Using Debugging tools

You can control the execution of a program by

  1. Setting breakpoints- Breakpoints causes the execution of thread should suspend at the location where breakpoint is set.
  2. Stepping through the code- Stepping through the code enables step-by-step debugging when debugging the objects.
  3. Examining the contents of the application -It is possible Examine the contents of application when debugging the application.
  4. Watch points- These are used to set breaks when object is modified. it can be set to break on variable access, variable modification or both.

Using System.out.println ()

System.out.println () can be used at various steps in your JSP and check the results or flow of your program based on the print statements. Let us understand the details of System.out.println ():

  • System is a built in class present in java.lang package. It contains pre-defined methods. These methods provide facilities like standard input, output and more.
  • out is static final field in System class which is of type PrintStream class.
  • println() is a public method in PrintStream to print values. To access in PrintStream class we use out.println().

Lisitng 1:System.out.println () example

<html>
   <body>
   <%
      Java.util.Date date=new java.util.Date();
      System.out.println (date);
   %>
   </body>
</html>

By using System.out.println (), it will display result in console and if we are using out.println (), it will display result on browser as html page.

Using JDK Logger

JDK Logger can be used to log messages for specific system or application component. The package java.util.logging provides the classes and interfaces of the Java’s core logging facilities.This can be used in JSP for debugging.Let see this in an example below:

Lisitng 2:JDK Logger example

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

<html>
   <head><title>Logger Example</title></head>
   <body>
   <% Logger logger=Logger.getLogger(this.getClass().getName());%>

   <c:forEach var="toll" begin="1" end="10" step="1" >
      <c:set var="mytoll" value="${toll-5}" />
      <c:out value="${mytoll}"/></br>
      <% String message = "toll="
                  + pageContext.findAttribute("toll")
                  + " mytollt="
                  + pageContext.findAttribute("mytoll");
                  logger.info( message );
      %>
   </c:forEach>
   </body>
</html>

Output screen:
jsp_debug_demo
Output on console:

Jan 22, 2014 10:03:16 AM org.apache.jsp.jskdloggerexample_jsp _jspService
INFO: toll=1 mytollt=-4
Jan 22, 2014 10:03:16 AM org.apache.jsp.jskdloggerexample_jsp _jspService
INFO: toll=2 mytollt=-3
Jan 22, 2014 10:03:16 AM org.apache.jsp.jskdloggerexample_jsp _jspService
INFO: toll=3 mytollt=-2
Jan 22, 2014 10:03:16 AM org.apache.jsp.jskdloggerexample_jsp _jspService
INFO: toll=4 mytollt=-1
Jan 22, 2014 10:03:16 AM org.apache.jsp.jskdloggerexample_jsp _jspService
INFO: toll=5 mytollt=0
Jan 22, 2014 10:03:16 AM org.apache.jsp.jskdloggerexample_jsp _jspService
INFO: toll=6 mytollt=1
Jan 22, 2014 10:03:16 AM org.apache.jsp.jskdloggerexample_jsp _jspService
INFO: toll=7 mytollt=2
Jan 22, 2014 10:03:16 AM org.apache.jsp.jskdloggerexample_jsp _jspService
INFO: toll=8 mytollt=3
Jan 22, 2014 10:03:16 AM org.apache.jsp.jskdloggerexample_jsp _jspService
INFO: toll=9 mytollt=4
Jan 22, 2014 10:03:16 AM org.apache.jsp.jskdloggerexample_jsp _jspService
INFO: toll=10 mytollt=5

Using Comments

Comments can be used in debugging process of a JSP.

  1. You can use single line (//…) comments
  2. multiline comments (/*….*/) comment

Both the above types can be used remove parts of code temporarily. The Jsp comment doesn’t appear in the output produced by the Jsp pages. Jsp comments do not increase size of the file.

Previous Tutorial : How To Use JavaBeans in JSP || Next Tutorial : Auto Refresh JSP Page

Comments

comments

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Speak Your Mind

*