JSP Debugging: How to Debug JSP in Eclipse?

⚡ Smart Summary

Debugging in JSP is the process of tracing errors in a JSP application. This resource explains three debugging techniques—println statements, the Java Logger, and debugger tools—and walks through how to debug a JSP page in Eclipse using breakpoints and debug mode.

  • 🐞 Definition: Debugging traces errors that are hard to reproduce in client-interactive JSP applications.
  • 🖨️ println: System.out.println() prints variable values to confirm which code lines executed.
  • 📝 Java Logger: The logging framework records leveled messages such as info and severe to log files.
  • 🔧 Debugger Tools: Eclipse provides breakpoints and a variables view to inspect runtime state.
  • ▶️ Eclipse Steps: Set breakpoints, restart the server in debug mode, then step through the code.

JSP Debugging

What is Debugging in JSP?

Debugging in JSP is the process of tracing the error in the application. It is not very easy to trace bugs and errors in the application. JSP applications are client-interactive. Hence, errors are difficult to reproduce.

JSP Debugging Techniques

There are 3 different techniques through which we can debug a JSP application:

  • Using println statements
  • Using Java Logger
  • Using Debugger tools

Using println Statements

  • System.out.println() is used to trace whether a certain part is executed or not.
  • We can print the variables in the code.
  • Also, we can print values that need to be used to debug the application.

Example:

In this example, we are debugging through the println statements to understand what values we are getting for the variables.

<%@ 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>Debug Guru JSP</title>
</head>
<body>
<% int num1 = 10;
System.out.println("This is debugging Guru JSP");
num1++;
int num2 = num1 / 0;
System.out.println(num2); %>
</body>
</html>

Explanation of the code:

Code Line 10: We are taking the variable num1 and initializing it to 10.

Code Line 11: We are printing, using the println statement, “This is debugging Guru JSP” to debug which line of code has been executed.

Code Line 12: Here we are incrementing the variable num1 by 1.

Code Line 13: We are dividing num1 by 0 and putting it into a new variable num2.

Code Line 14: As we want to know the value for num2, we can fetch it using the println statement to know up to which line of code has been executed.

When you execute the above code, you get the following output:

JSP Debugging Using println Statements

Output:

Here, we are getting the statement “This is debugging Guru JSP” with the value of the variable num1, which has been incremented, hence 11.

Using Java Logger

  • The Java logging framework is used for logging services for any class running in the JVM.
  • This is used to log any information from the code.
  • It logs all necessary information to trace the bugs that have occurred.

Example:

In this example, we are using the Java logger to trace the information in the code. Messages can be shown using different functions like severe(), warning(), info(), config(), and fine(). Info is used to show the information in the log file. Severe is used to show some severe information in the log file.

<%@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>Debugging Guru JSP</title>
</head>
<body>
<% Logger logger = Logger.getLogger(this.getClass().getName()); %>
<% Date dt = new Date();
logger.info(dt.toString());
logger.info("This is Guru Logging debugger"); %>
</body>
</html>

Explanation of the code:

Code Line 2: We are importing the Java logger to log the information.

Code Line 3: We are importing the Date class of the util package.

Code Line 13: We are initializing the logger class by using the getLogger method.

Code Line 14: We are initializing the Date class.

Code Line 15: We are using the info method of the logger class object to print the current date.

Code Line 16: We are using the info method to print ‘This is Guru Logging debugger’.

When you execute the above code, you will get the following output:

JSP Debugging Using Java Logger

Output:

We will get the output in std.log, which will be stored in the Logging folder on the server. Here we will get the information that has been written in the code.

Using Debugging Tools

We have been using Eclipse since the beginning of the tutorial. In Eclipse, we have debugging tools to debug the errors or bugs in the code.

How To Debug JSP in Eclipse

Here are the steps to debug a JSP application in Eclipse using debugging tools:

  1. Set the Breakpoint.
  2. Restart the Server in Debugging Mode.
  3. Debug through Breakpoints.

Step 1) Set the Breakpoint

We can set the breakpoint in the code, where we want to trace the code:

<%@ 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>Debugging Guru JSP</title>
</head>
<body>
<% String name = "This is test Guru JSP";
out.println(name);
%>
</body>
</html>

Here we are setting the breakpoint as ‘toggle breakpoint’ when you right-click on the project explorer. We can set the breakpoints where the error or bug is occurring. Once they are set, the debugger will move to the first breakpoint and then the next, and so on. It will run through the application with the help of breakpoints. After setting the breakpoint, we need to restart the server in debug mode.

Step 2) Restart Server in Debugging Mode

We can restart the server in debugging mode by clicking on the server side and clicking on Start in Debug mode.

Debug JSP in Eclipse

Step 3) Debug through Breakpoints

We can debug the application by clicking on the application as Debug As:

Debug JSP in Eclipse

You get an alert to switch the perspective; then click the “Yes” button. After clicking the ‘Yes’ button, we will get the following perspective:

Debug JSP in Eclipse

We will get the Variables tab, where we can see the values of the variables:

Debug JSP in Eclipse

The Breakpoints tab shows the number of breakpoints in the code:

Debug JSP in Eclipse

We can debug the application through the breakpoints that have been set.

FAQs

Yes. AI assistants can analyze stack traces, spot null pointers or division errors, and suggest fixes in JSP and servlet code. This speeds up debugging, but you should confirm the fix by running the page.

No. AI suggests likely causes, but stepping through breakpoints in Eclipse still gives precise runtime variable values that confirm the actual issue. The two approaches work best together.

println prints directly to the console and is hard to manage at scale. A logger records leveled messages, such as info or severe, to log files that can be filtered, retained, and analyzed later.

A 500 error usually comes from an uncaught exception in the JSP, such as a NullPointerException or division by zero. The exact cause is shown in the server logs and stack trace.

Summarize this post with: