beginnersbook.com

  • Home
  • All Tutorials
    • Learn Servlet
    • Learn JSP
    • Learn JSTL
    • Learn C
    • Learn C++
    • Learn MongoDB
    • Learn XML
    • Learn Python
    • Learn Perl
    • Learn Kotlin
    • Learn jQuery
  • Core Java
  • OOPs
  • Collections
  • Java I/O
  • JSON
  • DBMS

Exception Implicit Object in JSP with examples

By Chaitanya Singh | Filed Under: JSP tutorial

In this tutorial, we will discuss exception implicit object of JSP. It’s an instance of java.lang.Throwable and frequently used for exception handling in JSP. This object is only available for error pages, which means a JSP page should have isErrorPage to true in order to use exception implicit object. Let’s understand this with the help of below example –

Exception implicit Object Example

In this example we are taking two integer inputs from user and then we are performing division between them. We have used exception implicit object to handle any kind of exception in the below example.

index.html

<html>
<head>
<title>Enter two Integers for Division</title>
</head>
<body>
<form action="division.jsp"> 
Input First Integer:<input type="text" name="firstnum" />
Input Second Integer:<input type="text" name="secondnum" /> 
<input type="submit" value="Get Results"/> 
</form>
</body>
</html>

Here we have specified exception.jsp as errorPage which means if any exception occurs in this JSP page, the control will immediately transferred to the exception.jsp JSP page. Note: We have used errorPage attribute of Page Directive to specify the exception handling JSP page (<%@ page errorPage=”exception.jsp” %>).

division.jsp

<%@ page errorPage="exception.jsp" %> 
<% 
String num1=request.getParameter("firstnum"); 
String num2=request.getParameter("secondnum"); 
int v1= Integer.parseInt(num1);
int v2= Integer.parseInt(num2);
int res= v1/v2;
out.print("Output is: "+ res);
%>

In the below JSP page we have set isErrorPage to true which is also an attribute of Page directive, used for making a page eligible for exception handling. Since this page is defined as a exception page in division.jsp, in case of any exception condition this page will be invoked. Here we are displaying the error message to the user using exception implicit object.

exception.jsp

<%@ page isErrorPage="true" %> 
Got this Exception: <%= exception %> 
Please correct the input data.

Output

Screen with two input fields for two integer numbers.

inputPage

Arithmetic Exception message when we have provided the second integer as zero.

exceptionPage

Let us know if you have any questions regarding exception implicit object in JSP. This is one of the most frequently used implicit object while building an application in JSP. As an alternative, you can also handle exceptions in JSP by using try catch within JSP scriptlet.

Comments

  1. Prahald sahu says

    July 27, 2014 at 10:59 AM

    Chaitanya Singh , Thank You sir. Mind blowing explanation, Site name is simply beginnersbook.com , if beginners will read these post carefully then they can give all answers of related 3yrs interview question.

    Reply
  2. Ranadheer kumar says

    July 7, 2016 at 12:59 PM

    you provide very good information all about implicit objects. If it is possible please send methods and example programs about session object and page object.

    Reply

Leave a Reply Cancel reply

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

JSP Tutorial

  • Basics of JSP
  • Life cycle of JSP
  • JSP in Eclipse IDE
  • Declaration tag
  • Expression tag
  • Scriptlets
  • Directives
  • Include Directive
  • Param Directive
  • Exception handling
  • Action tags
  • Include action
  • Forward action
  • useBean, setProperty & getProperty
  • Implicit Objects
  • Session implicit object
  • Validate session
  • Request implicit object
  • Response implicit object
  • Out implicit object
  • Application implicit object
  • Config implicit object
  • pageContext implicit object
  • Exception implicit object
  • Expression language
  • Custom Tags
  • Custom tag example
  • JSP Interview Q

Recently Added..

  • JSON Tutorial
  • Java Regular Expressions Tutorial
  • Java Enum Tutorial
  • Java Annotations Tutorial

Copyright © 2012 – 2022 BeginnersBook . Privacy Policy . Sitemap