JSP Error Page is used to specify the custom error page and runtime error occurs with an exception being thrown, the custom error page helps in handling the error page and display a customized view of the exception.
Understand with Example
In this section, you will learn the use of errorPage attribute of the page
directive in jsp. This attribute sets a url and if any exception is generated it
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.
Here we have used two jsp pages:
In the error.jsp page, we have initialized a string variable to null in a scriplet and use the method st.length() inorder to determine the length of the string which is the cause of generating an error. This page is redirected to the errorPage.jsp as specified in the errorPage attribute of the page directive. The page errorPage.jsp shows the error generated in calling the JSP page by setting the isErrorPage attribute to true.
Here is the code of error.jsp
|<[email protected] errorPage="errorPage.jsp" %>
String st = null;
The length of the string is <%= st.length() %>
Here is the code of errorPage.jsp
|<%@ page isErrorPage="true" import="java.io.*" %>
<font color="red"><%= exception %> has been encountered. You haven't specify any String.</font>
Output will be displayed as:
Posted on: April 18, 2011 If you enjoyed this post then why not add us on Google+? Add us to your Circles