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.
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
- Using println statements
- Using JDB Logger
- Using Debugging Tools
- Using JDB Debugger
- 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.
<%@ 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>
You will get the following output:
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”.
<%@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>
You will get the following output in the std.log file in the server.
JSP Debudding Using Debugging Tools
We have debugging tools in eclipse to debug the errors in the code. We will following steps:
- Set the breakpoint
- Restart the server in debugging mode
- Debug through breakpoints
Set the breakpoint
Set the breakpoint where you want to trace the code.
<%@ 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.
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”.
Debug through breakpoints
We can debug the application by right click on the code, then go to “Debug AS -> Debug on Server” as follows:
You will get the following Perspective pop-up, then click on “Switch”
The debug view will be as follows:
The variable view will be as follows:
The breakpoints will be as follows:
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.
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.
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:
- User-Agent: It contains information about the version of browser/ client software.
- 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?
- Give forwarding location
- Specify cookies
- Supply the page modification date
- Instruct the browser to reload the page after a designated interval
- Give the file size so that persistent HTTP connections can be used.
- 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.