The following example illustrates creating error pages in JSP. As the name suggests, error pages are those pages that will be viewed to the user when an error occurs in a jsp page.
For example, when an exception occurs in a JSP page, you'll get the default tomcat (or any server) generated error page which contains the entire details of where the error occurred etc. In case if you don't want that to be shown to the user, you can use the error pages.
There is one tag that you'll be using with two different attributes in two different contexts. We will be using the @page directive. The syntax will for this directive is as follows
Now, the first context is using the attribute errorPage whose value must be the jsp page that displays the error.
This tag should be written in the JSP page where the exception will occur.
The next context is using the attribute isErrorPage whose value must be true for error pages.
This tag must be placed in the error page.
The following example will add two numbers. A NumberFormatException is raised if the user enters non integer values in the textfields. When such an exception occurs, an error page is shown.
For example, when an exception occurs in a JSP page, you'll get the default tomcat (or any server) generated error page which contains the entire details of where the error occurred etc. In case if you don't want that to be shown to the user, you can use the error pages.
There is one tag that you'll be using with two different attributes in two different contexts. We will be using the @page directive. The syntax will for this directive is as follows
<%@page attr="value"%>
Now, the first context is using the attribute errorPage whose value must be the jsp page that displays the error.
<%@page errorPage="myerr.jsp"%>
This tag should be written in the JSP page where the exception will occur.
The next context is using the attribute isErrorPage whose value must be true for error pages.
<%@page isErrorPage="true"%>
This tag must be placed in the error page.
The following example will add two numbers. A NumberFormatException is raised if the user enters non integer values in the textfields. When such an exception occurs, an error page is shown.
index.jsp - Takes input of 2 numbers
<html>
<body>
<form action="add.jsp">
Enter first number: <input type="text" name="t1"/>
Enter second number: <input type="text" name="t2"/>
<input type="submit" value="Add"/>
</form>
</body>
</html>
add.jsp - prints the sum
<%@page errorPage="myerror.jsp"%>
<%
int x=Integer.parseInt(request.getParameter("t1"));
int y=Integer.parseInt(request.getParameter("t2"));
out.println("The sum is "+(x+y));
%>
myerror.jsp - displays the error message
<%@page isErrorPage="true"%>
<h1>Please type your input properly</h1>
<b>
<%=exception%>
</b>
Here, if the isErrorPage is not true or if the tag is omitted, then you'll not get the exception object used here. However, if you do not use the exception object you need not write the tag.
Note: You cannot declare multiple error pages for single JSP page. All of the exceptions (aka errors in this context) must be directed to an error page which gets the exception object. Depending upon the exception you get, the jsp page will display a corresponding message or do a corresponding action.
Previous: Using @page import