JSP Error Page

This page discusses - JSP Error Page

JSP Error Page

JSP Error Page

        

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:
1) error.jsp
2) errorPage.jsp

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

<%@page errorPage="errorPage.jsp" %>
<html>
<head><title>Error Page</title></head>
<body>
<%! 
String st = null;
%>
The length of the string is <%= st.length() %>
</body>
</html>

Here is the code of errorPage.jsp

<%@ page isErrorPage="true" import="java.io.*" %>
<html>
<body>
<h2>Error Encountered!</h2>
<font color="red"><%= exception %> has been encountered. You haven't specify any String.</font>
</body>
</html>

Output will be displayed as:

Download Source Code: