Debugging in JSP

Debugging in JSP Applications with Examples

In this article, I am going to discuss Debugging in JSP Applications with Examples. Please read our previous article where we discussed MVC in JSP Application with Examples.

JSP Debugging

It is the process to trace the error in the application which is not that easy to trace bugs and errors because JSP applications involve a large amount of client interaction so the errors are difficult to reproduce.

Different Debugging Methods
  1. Using println statements
  2. Using JDB Logger
  3. Using Debugging Tools
  4. Using JDB Debugger
  5. Using Comments
JSP Debugging Using println statements

It is used to test whether a certain piece of code is executed or not. Also, we can print variable values which need to be used to debug the applications.

Example Using println statements

In this example, we are using println statement to debug the code. We are printing “Using println statement” to understand which line of code has been executed. Here we are taking variable num1 and initialized it to 10 and every time we are incrementing its value by 1. We are dividing the num1 by 0 and storing it in num2 to know which line of code has been executed.

println.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
 pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>JSP Debugging </title>
</head>
<body>
 <%
 int num1 = 10;
 System.out.println("Using println Statement");
 num1++;
 int num2 = num1 / 0;
 System.out.println(num2);
 %>
</body>
</html>
Output

You will get the following output:

JSP Debugging Using println statements

JSP Application Debugging Using JDB Logger

It is a framework used for logging services for any class running in JVM. It is used to log any information from the code to trace the bugs which have occurred.

Example Using JDB Logger

In this example, we are using the JDB logger to trace the information. Here we are importing the java logger and date class of the util package. Then we are initializing the date class and logger class by using the getLogger method. Then we are using the info method to print the current date and the message “JDB Logger Debugging”.

JDBLogger.jsp
<%@taglib prefix="guru" uri="http://java.sun.com/jsp/jstl/core" %>
<%@page import="java.util.logging.Logger" %>
<%@page import="java.util.Date" %>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>JSP Debugging </title>
</head>
<body>
<% Logger logger=Logger.getLogger(this.getClass().getName());%>
<% Date dt = new Date();
logger.info(dt.toString());
logger.info("JDB Logger Debugging"); %>
</body>
</html>
Output:

You will get the following output in the std.log file in the server.

JSP Application Debugging Using JDB Logger

JSP Debudding Using Debugging Tools

We have debugging tools in eclipse to debug the errors in the code. We will following steps:

  1. Set the breakpoint
  2. Restart the server in debugging mode
  3. Debug through breakpoints
Set the breakpoint

Set the breakpoint where you want to trace the code.

Debugging.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
 pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>JSP Debugging</title>
</head>
<body>
 <%
 String name = "JSP Debugging through breakpoint";
 out.println(name);
 %>
</body>
</html>

To set the breakpoint right click on the info bar and select “Toggle Breakpoint”. Set the breakpoints where the error is occurring.

Set the breakpoint

The debugger will then move to the first breakpoint, then next, and so on till the end of the application. Once the breakpoint is set, restart the server in debug mode.

Restart server in debug mode

To restart the server in debug mode, right-click on the server go to “Restart in Debug”.

Restart server in debug mode

Debug through breakpoints

We can debug the application by right click on the code, then go to “Debug AS -> Debug on Server” as follows:

Debug through breakpoints

You will get the following Perspective pop-up, then click on “Switch”

JSP Debugging Using Debugging Tools

The debug view will be as follows:

Debugging in JSP Applications with Examples

The variable view will be as follows:

Debugging in JSP Applications

The breakpoints will be as follows:

Debugging in JSP

JSP Application Debugging Using JDB Debugger

It is a tool for Java classes to debug a program in the command line by implementing the Java Platform Debugger Architecture and helps in detecting and fixing bugs in a Java program using Java Debug Interface.

We can debug a JSP by using the sun.servlet.http.HttpServlet and then executes the HttpServer in response to HTTP requests made from a browser. Now set the debugger’s classpath which helps you find the sun.servlet.http.Http-Server, your JSP, and the associated classes ROOT\WEB-INF\classes.

Once you set the proper classpath, first set the breakpoints in JSP which you want to debug, then use the web browser to make a request to the HttpServer sun.servlet.http.HttpServlet for the given JSP. Here the execution stops at the breakpoint.

Using Comments

We can also comment on the Java code which we want to debug. JSP can use single-line comment (//…..) as well as Multi-line comments(/* ….. */) which can temporarily remove parts of your Java code. Then take a closer look at the code if the bug disappears and find out the problem.

Client and Server Headers

The client and the server pass additional information with an HTTP request or response headers. A client is a machine that sends the request to the server. A server is a machine that receives the request and processes the request and sends back the result to the client.

Client and Server Headers

HTTP Request Headers

HTTP requests include headers that provide extra information about the request. Headers are used to send the extra information about the client to the server like what is the version of the browser or client software, what language is used by the client, etc. Following are the two most important headers:

  1. User-Agent: It contains information about the version of browser/ client software.
  2. Accept-Language: It contains information about what language is used by the client.
HTTP Response Header

Headers are used to send the extra information to the client. The most important Header send by the server to the client is “contentType(text/html)”. Sometimes we also send error reports, header-cache into, etc.

Why HTTP Response Headers?
  1. Give forwarding location
  2. Specify cookies
  3. Supply the page modification date
  4. Instruct the browser to reload the page after a designated interval
  5. Give the file size so that persistent HTTP connections can be used.
  6. Designate the type of document being generated.

In the next article, I am going to discuss JSP Exception Handling with Examples. Here, in this article, I try to explain Debugging in JSP Application with Examples and I hope you enjoy this Debugging in JSP Application with Examples article.

Leave a Reply

Your email address will not be published.