"errorPage" and "isErrorPage" are the two of 14 attributes supported by page directive.
Now let us what are errorPage isErrorPage exception JSP
1. What is "errorPage"?
Any JSP file declared with errorPage, when generates exceptions, can send the exceptions to another JSP file that is declared with isErrorPage. errorPage attribute is used to specify the address of another JSP file where isErrorPage is set to true. errorPage and isErrorPage go together.
Syntax: <%@ page errorPage="URL of other JSP file" %>
2. What is "isErrorPage"?
Any JSP file declared with isErrorPage (and set to a value of true), is capable to receive exceptions from other JSP pages. Implicit object exception is available to these pages only (set with true). Default value is false.
Syntax: <%@ page isErrorPage="true" %>
It should be noted that errorPage and isErrorPage are used in combination with "exception" object.
The exception object is used to print the exception message at runtime in JSP file, or to say, exception object represents errors and exceptions.
Now let us write a small application to get the concept. The application includes 3 programs and let us see how they work together.
- The TwoNumbers.html of client-side file reads two numbers from client and send to Calculate.jsp for calculating quotient.
- The Calculate.jsp can generate exceptions at runtime due to user’s wrong input. The exceptions raised are sent to Receive.jsp file to print.
- Declare the Calculate.jsp file with errorPage="Receive.jsp" in a page directive.
- Declare the the Receive.jsp file with isErrorPage="true" in a page directive.
Finally, now Calculate.jsp file can send exceptions to Receive.jsp and Receive.jsp accepts them and print them. As long as TwoNumbers.html sends two non-zero numbers, it is okay and good. But if the second number is 0, it raises ArithmeticException. This ArithmeticException object is sent to Receive.jsp to print.
1. Client HTML file, File Name: TwoNumbers.html
2. Server-side JSP, File Name: Calculate.jsp
This file which sends exception object to Receive.jsp file.
<%@ page errorPage="Receive.jsp" %> <% int fn = Integer.parseInt(request.getParameter("t1")); int sn = Integer.parseInt(request.getParameter("t2")); %>
Your first number is <%= fn %> and second number is <%= sn %>..
Quotient is <%= fn/sn %>
If the above code, if sn is 0, then it generates ArithmeticException. This exception object is sent to Receive.jsp for printing.
3. Server-side JSP, File Name: Receive.jsp
<%@ page isErrorPage="true" %>
Sorry, Quotient cannot be printed.
Cause of problem: <%= exception %>
HTML screen when non-zero number 10 is given as second number.
Screenshot when second number is given a non-zero number. Response send by Calculate.jsp
Screenshot when second number is given zero. Response send by Receive.jsp
3. What methods "exception" object can make use of?
These methods are nothing but defined in java.lang.Throwable class of which important are getMessage(), printStackTrace() and toString().