JSP isErrorPage Example

JSP isErrorPage Example


Posted in : Java Posted on : May 2, 2012 at 6:18 PM Comments : [ 0 ]

In this tutorial you will learn about the attribute of page directive isErrorPage.

JSP isErrorPage Example

In this tutorial you will learn about the attribute of page directive isErrorPage.

isErrorPage attribute of page directive is used to construct an error page. This attribute is used with the page directive at the beginning of the JSP page. Value of this attribute is either true or false. Web server identifies the error page if an attribute isErrorPage value is set to "true". In JSP if you want to handle the exceptions that occurs on the execution of the JSP page you may use the page directive attribute isErrorPage="true", on this page you can handle the exceptions. A page containing the page directive attribute isErrorPage="true" is a page where the web container will forward the control from the exception prone page when the exception is thrown. The exception prone page should contain the page directive attribute errorPage="nameOfIsErrorPage.jsp".

Example :

An example is being given below will demonstrate you about the page directive attribute isErrorPage. In this example I have created a JSP page on which at the beginning used the page directive isErrorPage="true". Then find the exception class and the error message. Then I have created an another JSP page where I have tried to take the input from user and parse this number into integer then divide this number by zero. This statement will throw an arithmetic exception.

isErrorPage.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page isErrorPage="true" %>
<!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>errorPage</title>
</head>
<body>
<h3>Due to following reasons an error has occurred</h3>
<ul>
<li><%=exception.getClass() %></li>
<li><%=exception.getMessage() %></li>
</ul>
</body>
</html>

ExceptionHandlingPage.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page errorPage="errorPage.jsp" %>
<!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>Exception Handling</title>
</head>
<body>
<form>
<table>
<tr>
<td>Enter a number :</td>
</tr>
<tr>
<td><input type="text" name="num"/></td>
</tr>
<tr>
<td align="center"><input type="submit" value="submit"/>
</tr>
</table>
</form>
<%
String num = request.getParameter("num");
if(num != null)
{
String number = num.trim();
int no = Integer.parseInt(number);
out.println(no);
int value= no/0;
}
%>
</body>
</html>

Output :

1. When you will execute the ExceptionHandlingPage.jsp you will get the output as following :

2. When you will click on submit button the web container will be forward the control to the page where you have defined the error.

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics