JSP
JSP Expression Language (EL) Tutorial
What is Expression Language (EL)? Expression Language (EL) is mechanism that simplifies the...
Debugging is the process to trace the error in the application. It is not very easy to trace bugs and error in the application.
JSP applications are client interactive. Hence, errors are difficult to reproduce.
In this tutorial, you will learn-
There are different ways through which we can debug an 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 variable num1 and initializing it to 10.
Code Line 11: We are printing using println statements that "This is debugging Guru JSP" to debug which line of code has been executed
Code Line 12: Here we are incrementing the variable num1 with 1.
Code Line 13: we are dividing the num1 by 0 and putting into a new variable num2.
Code Line 14: As we want to know the value for num2 we can fetch using the println statement to know till which line of code has been executed.
When you execute the above code, you get the following output:
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.
Example:
In this example, we are using java logger to trace the information in the code.
Messages can be shown using different functions like severe(), warning(), info(), config(). Fine()
Info is used to show the information in the log file .
Severe is used to show some severe information on 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 Date class of the util package
Code Line 13: We are initializing the logger class by using getLogger method.
Code Line 14: We are initializing the date class.
Code Line 15: we are using the info method of 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
Output:
We will get the output in std.log, which will store in Logging folder in the server. Here we will get the information which has been written in the code.
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.
To debug the application we have following points:
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 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.
We can restart the server in debugging by clicking on the server side and click on start in debug mode.
We can debug the application by clicking on the application as debug as:
You get an alert to switch the perspective then click "Yes" button.
After clicking 'yes' button we will get the following perspective:
We will get the variables tab, where we can see values of the variables:
The breakpoints tab shows number of breakpoints in the code:
We can debug the application through the breakpoints which have been set.
Summary:
What is Expression Language (EL)? Expression Language (EL) is mechanism that simplifies the...
In this tutorial, we are going to study the basics of writing and running a JSP. We will install...
What is Maven? Maven is an automation and management tool developed by Apache Software Foundation. It...
What is JSP Action? JSP actions use the construct in XML syntax to control the behavior of the...
What is MVC? MVC is an architecture that separates business logic, presentation and data. In MVC, M...
Download PDF 1) Explain what is Maven? How does it work? Maven is a project management tool. It...