errorPage isErrorPage exception JSP page directive with Example

errorPage isErrorPage exception JSP page directive with Example

"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.

"exception" is one of the 9 implicit objects supported by Java. "exception" is an object of java.lang.Throwable, available to the programmer to be used directly in the code without creating it.

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.

  1. The TwoNumbers.html of client-side file reads two numbers from client and send to Calculate.jsp for calculating quotient.
  2. 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.
  3. Declare the Calculate.jsp file with errorPage="Receive.jsp" in a page directive.
  4. 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.

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

HTML screen when non-zero number 10 is given as second number.

ima

Screenshot when second number is given a non-zero number. Response send by Calculate.jsp

errorPage isErrorPage exception JSP

Screenshot when second number is given zero. Response send by Receive.jsp

errorPage isErrorPage exception 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().

Leave a Reply

Your email address will not be published. Required fields are marked *