Home Tutorial Java Jsp The "errorPage" & "isErrorPage" Attribute of Page Directive

 
 

Share on Google+Share on Google+
The "errorPage" & "isErrorPage" Attribute of Page Directive
Posted on: June 15, 2010 at 12:00 AM
Advertisement
In this Section, we will discuss about the "errorPage" & "isErrorPage" attribute of Page Directive.

The "errorPage" & "isErrorPage"  Attribute of Page Directive

In this Section, we will discuss about the "errorPage" & "isErrorPage"  attribute of Page Directive.

The "errorPage" Attribute

When an unhandled Exception occurs in JSP page , the mentioned "errorpage" in this attribute is called. The syntax of this Page directive is as follows :

     <%@page errorPage="relativeURL" %>

"Relative Url" is the path of JSP file, who handled the exception. This attribute sets a url (relative path starting from the "/" , which refers to the root directory of your JSP application). If any exception is generated the the attribute refers to the file which is mentioned in the given url. If you do not specify the url then the attribute refers to the current page of your JSP application if any exception generated.

The "isErrorPage" attribute 

The Syntax of this page directive is :

     <%@page isErrorPage="true | false"  %> 

It is a Boolean attribute , if you set it's value "true" ,you can use "Exception" object in your JSP page otherwise you cannot use the exception object because the default value of the attribute is false .

Example :  

In this Example, we incorporate two JSP pages, one (errorpage.jsp)for entering data &  for implementing logic and second one(error.jsp) is for handling "Exception". If any exception occurs "error.jsp" page handles it & display the exception occurred.

pageerror.jsp

<%@page errorPage="error.jsp" %>

<html>

<head><title>Showing Error Page.</title></head>

<body>

<form method="post">

<table border="0" cellspacing="0" cellpadding="0">

<tr>

<td>Enter a number: </td>

<td><input type="text" name="txtNum" />

</tr>

<tr>

<td>&nbsp;</td>

<td><input type="submit" name="B1" value="Divide by zero" />

</tr>

</table>

</form>

<%

if(request.getParameter("txtNum") != null){

if(!request.getParameter("txtNum").equals("")){

int div = Integer.parseInt(request.getParameter("txtNum")) / 0;

out.println("Answer is: " + div);

}

else{

out.println("<html><font color=red>Please enter a number.</font></html>");

}

}

%>

</body>

</html>

 

error1.jsp

<%@page isErrorPage="true" %>

<html>

<head><title>Error Page.</title></head>

<body bgcolor="blue">

<font size="5" color="white">Your page generated an error:"<br/>

Exception:<br/></font>

<%= exception.toString() %>

</body>

</html>

 

OTUPUT

After inputting number :

Download Source Code

Advertisement

Related Tags for The "errorPage" & "isErrorPage" Attribute of Page Directive:


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: June 15, 2010

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com