implicit object exception JSP Example

implicit object exception JSP Explanation Example Syntax

"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 cannot be used always and everywhere in the JSP code. It is specially used in combination with "errorPage" and "isErrorPage".

1. Where implicit object exception JSP can be used?

The exception object is used to print the exception message at runtime in JSP file, or to say, exception object represents errors and exceptions. Let us create a small scenario.

  1. 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. This is how to write in JSP coding.
  2. Write in the Calculate.jsp file like errorPage="Receive.jsp" in a page directive.
  3. Write in the Receive.jsp file like isErrorPage="true" in a page directive.

That is all. Now Calculate.jsp file can send exceptions to Receive.jsp and Receive.jsp accepts them and print them. But in general cases, exception object is not used in JSP coding (used with error pages only).

2. What are "errorPage" and "isErrorPage" in JSP?

They are attributes of page directive. Error page is used to print runtime errors.

"errorPage" in JSP can generate error at runtime. When generated it can be written to another JSP file which accepts by declaring "isErrorpage=true".

Following simple example contains 3 files. TwoNumbers.html file sends two numbers to Calculate.jsp file to find the quotient. This JSP file calculates the quotient and sends to the client. It is okay, but if the second number is 0, it raises ArithmeticException. This ArithmeticException object is sent to Receive.jsp to print.

Example on implicit object exception JSP

1. Client HTML file, File Name: TwoNumbers.html

Find Quotient

Enter First Number
Enter Second Number

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.

implicit object exception JSP

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

ima1

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

implicit object exception JSP

<%@ page xxxx %> is known as page directive and elaborately discussed in JSP Page Directive Tutorial – 14 Attributes and Examples.

3. What methods implicit object exception JSP 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 Comment

Your email address will not be published.