Error and exception handling in JSP:
We already know about error and exception.
Errors can occur in a JSP in two different phases of its life.
Translation time errors
JSP Request Time Error
Translation Time Error
• This type of Error occurs during the initial request. When a JSP page is
first requested and goes through the initial translation from
a JSP source file into a corresponding Servlet class file.
• These errors are usually the result of compilation failures and are
known as Translation Time Errors.
• They are reported to the requesting Client with an Error Status Code
500 or Server Error and usually contain the reported compilation
error.
• Translation Time Errors are handled by the JSP Engine.
JSP Request Time Error
• This type of Error occurs during subsequent requests. These Errors
are Run-Time errors that can occur in either the body of the JSP page
or in some other object that is called from the body of the JSP Page.
• Request Time Errors result in an Exception being thrown.
• These Exceptions can be caught and appropriately handled in the
body of the calling JSP, which would be the end of the error.
• Those Exceptions that are not caught result in the forwarding of
the Client request, including the Uncaught Exception, to the Error
Page specified by the offending JSP.
• Exception can be handled in JSP in three ways:
a) Java Exception Handling mechanism
b) Dealing with exception with page directive
c) Dealing with exception in Deployment Descriptor.
JSP Exception handling with try catch:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>[Link]</title>
</head>
<body>
<form action="../JSPFILE/[Link]">
<input type="text" name="n1"> <br>
<input type="text" name="n2"> <br>
<input type="submit" value="ADD">
</form>
</body>
</html>
<%@ page language="java" import="[Link].*" pageEncoding="ISO-8859-
1"%>
<html>
<head>
<title> [Link] </title>
</head>
<body>
<% try
{
int i1 = [Link]([Link]("n1"));
int i2 = [Link]([Link]("n2"));
int add = i1 + i2;
[Link]("Addition = "+add);
}
catch(NumberFormatException ne)
{
[Link]("Esception : "+ne);
}
%>
</body>
</html>
• Input the integer value in text fields and click ADD button.
• The browser display the below message,
• Addition of 6+5=11
• Now, input the float value in any of the text field and click ADD button
so the browser display the message.
• Esception : [Link]: For input string: "6.3"
Jsp exception handling with page directive:
The two attributes of page directive errorPage and isErrorPage are used to deal
with exception.
DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<!
<html>
<head>
<title>[Link]</title>
</head>
<body>
<form action="[Link]">
<input type="text" name="n1"> <br>
<input type="text" name="n2"> <br>
<input type="submit" value="ADD">
</form>
</body>
</html>
[Link]
<%@ page language="java" import="[Link].*" pageEncoding="ISO-8859-1" errorPage="[Link]"%>
<html>
<head>
<title> [Link] </title>
</head>
<body>
<%
int i1 = [Link]([Link]("n1"));
int i2 = [Link]([Link]("n2"));
int add = i1 + i2;
[Link]("Addition = "+add);
%>
</body>
</html>
[Link]
<%@ page language="java" import="[Link].*" pageEncoding="ISO-8859-1" isErrorPage="true"%>
<html>
<head>
<title>[Link]</title>
</head>
<body>
Your page generate an Exception. <br>
<%= [Link]() %>
</body>
</html>
• Input the integer value in textfields and click ADD button.
• The browser display the below message,
• Addition of 6+5=11
• Now, input the float value in any of the textfield and click ADD button
so the browser display the message,
• Your page generate an Exception.
For input string: "6.3"
Exception handling in jsp with deployment descriptor:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>[Link]</title>
</head>
<body>
<form action="../JSPFILE/[Link]">
<input type="text" name="n1"> <br>
<input type="text" name="n2"> <br>
<input type="submit" value="ADD">
</form>
</body>
</html>
[Link]
<%@ page language="java" import="[Link].*" pageEncoding="ISO-8859-1"%>
<html>
<head>
<title> [Link] </title>
</head>
<body>
<%
int i1 = [Link]([Link]("n1"));
int i2 = [Link]([Link]("n2"));
int add = i1 + i2;
[Link]("Addition = "+add);
%>
</body>
</html>
[Link]
<%@ page language="java" import="[Link].*" pageEncoding="ISO-8859-1" isErrorPage="true"%>
<html>
<head>
<title>[Link]</title>
</head>
<body>
Your page generate an Exception. <br>
<%= [Link]() %>
</body>
</html>
[Link]
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="[Link]
xmlns:xsi="[Link]
xsi:schemaLocation="[Link]
[Link]
<welcome-file-list>
<welcome-file>[Link]</welcome-file>
</welcome-file-list>
<error-page>
<exception-type>[Link]</exception-type>
<location>/[Link]</location>
</error-page>
</web-app>
• NOTE : [Link] (deployment descriptor) file is available in WEB-INF folder at WebRoot.
• Input the integer value in textfields and click ADD button.
• The browser display the below message,
• Addition = 11
• Now, input the float value in any of the textfield and click ADD button so the browser
display the message,
• Your page generates an Exception.
For input string: "6.3"
• This deployment descriptor entry means that whenever a web component throws a
NumberFormatException from any web page in the whole application(web project), the
web container call the [Link] file, which simply reports the error message in web
browser.