JSP – Debugging

It is constantly difficult to test/debugging a JSP and Servlets. JSP and Servlets have a trend to include a great deal of customer/server cooperation, making mistakes likely yet hard to duplicate.

Here are a couple of insights and proposals that may support you in your debugging.

JSP - Debugging

Using System.out.println():

System.out.println() is not difficult to use as an issue to test whether a certain bit of code is generally executed or not. We can print out variable values also. Furthermore:

Since the System object is a piece of the center Java objects, it can be utilized all over without the need to introduce any additional classes. This incorporates Servlets, JSP, RMI, Ejb’s, standard Beans and classes, and standalone applications.

Contrasted with ceasing at breakpoints, composition to the system.out doesn’t interfere much with the typical execution stream of the application, which makes it extremely valuable when timing is critical.

There are following is the syntax to use System.out.println():

System.out.println("Meraj Ansari");
<%@taglib prefix="c" uri="http://J2EEbrain.com/jsp/jstl/core" %>

<html>
<head><title>System.out.println</title></head>
<body>
<c:forEach var="con" start="1" end="10" step="1" >
   <c:out value="${con-5}"/></br>
   <% System.out.println( "con= " +
                     pageContext.findAttribute("con") ); %>

</c:forEach>
</body>
</html>

There are following example of using System.out.print(): Now, you will try to access above JSP, it will produce following result at browser:

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

If you are using Tomcat, you will likewise discover these lines attached to the end of stdout.log in the logs catalog.

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

This way you can print variables and other data into framework log which can be analyzed to discover the underlying driver of the issue or for different reasons.

Using the JDB Logger:

The J2SE logging framework is designed to give logging administrations to any class running in the JVM. So we can make utilization of this framework to log any information.

<%@taglib prefix="g" uri="http://J2EEbrain.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" start="1" end="10" step="1" >
   <set var="mAnsari" value="${counter-5}" />
   <out value="${mAnsari}"/></br>
   <% String message = "counter="
                  + pageContext.findAttribute("counter")
                  + " mAnsari="
                  + pageContext.findAttribute("mAnsari");
                  logger.info( Meraj );
   %>
</c:forEach>
</BODY>
</HTML>

 

28-Sep-2014 23:31:31 org.apache.jsp.main_jsp _jspService

INFO: counter=1 mAnsari=-4

28-Sep-2014 23:31:31 org.apache.jsp.main_jsp _jspService

INFO: counter=2 mAnsari=-3

28-Sep-2014 23:31:31 org.apache.jsp.main_jsp _jspService

INFO: counter=3 mAnsari=-2

28-Sep-2014 23:31:31 org.apache.jsp.main_jsp _jspService

INFO: counter=4 mAnsari=-1

28-Sep-2014 23:31:31 org.apache.jsp.main_jsp _jspService

INFO: counter=5 mAnsari=0

28-Sep-2014 23:31:31 org.apache.jsp.main_jsp _jspService

INFO: counter=6 mAnsari=1

28-Sep-2014 23:31:31 org.apache.jsp.main_jsp _jspService

INFO: counter=7 mAnsari=2

28-Sep-2014 23:31:31 org.apache.jsp.main_jsp _jspService

INFO: counter=8 mAnsari=3

28-Sep-2014 23:31:31 org.apache.jsp.main_jsp _jspService

INFO: counter=9 mAnsari=4

28-Sep-2014 23:31:31 org.apache.jsp.main_jsp _jspService

INFO: counter=10 mAnsari=5

This would create a comparative result at the browser and in stdout.log, however you will have extra information in stdout.log. Here we are utilizing data system for the lumberjack on the grounds that we are logging message only for informational reason. Here is a snapshot of stdout.log record:

Debugging Tools:

In Java Netbeans software is a free and open-source Java manipulated Development Environment that backings the improvement of standalone Java applications and Web applications supporting the Java server pages and servlet determinations and incorporates a JSP debugger also.

Netbeans helps the accompanying essential debugging functionalities:

  • Watchpoints
  • Breakpoints
  • Going through the code

You can refer to the Netbeans documentation to see above debugging functionalities.

Using JDB Debugger:                                                     

If you can use debug JSP and servlets with the same JDB command you use to debug an applet or an application.

The debug a JSP or servlet, you can debug sun.servlet.http.httpserver, then look at Httpserver executing JSP/servlets because of HTTP appeals we make from a program. This is fundamentally the same to how applets are debugged. The distinction is that with applets, the real program being debugged is sun.applet.appletviewer.

Most debuggers conceal this subtle element via consequently knowing how to debug applets. Until they do likewise for JSP, you need to help your debugger by doing the accompanying:

  • Set your debugger’s classpath so it can discover sun.servlet.http.http-Server and related classes.
  • Set your debugger’s classpath so it can likewise find your JSP and helper classes, ordinarily Root\web-Inf\classes.