In this tutorial you will learn about the JSP Exception Implicit Object and its application with practical example.
JSP Exception Implicit Object
The exception object describing the error is a java.lang.Exception instance that is accessible in the error page through the implicit exception object. Only an error page can access the implicit exception object. When the error condition occurred so this implicit object is used to generate an appropriate response. In other words Exception is normally an object which is thrown at runtime, With the help of Exception Handling we can handle runtime process.
In JSP we can perform exception handling by two way:
#Page Directive: With the help of this you can simply create an jsp page and call as directive tag in your processing page.
1 |
<%@ page errorPage="exception.jsp" %> |
#By specifying the error-page element in web.xml: If you use this approach for handling error so you do not need to call page directives on each jsp, because you have declared in web.xml file, like below code.
1 2 3 4 5 6 |
<web-app> <error-page> <exception-type>java.lang.Exception</exception-type> <location>/exception.jsp</location> </error-page> </web-app> |
Example: Let’s try exception handling using “errorPage an isErrorPage attributes of page directive“.First we need to create jsp page for an exception.
1.exception.jsp: This page will called when an exception will occurred. Check out below code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<%@page contentType="text/html" pageEncoding="UTF-8"%> <%@ page isErrorPage="true" %> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>W3Adda Exception Page</title> </head> <body> <h3>Sorry an exception occurred!</h3> Exception is: <%= exception%> </body> </html> |
2.index.jsp: This page will appear when you will run, from here you have to enter numbers so that you can divide, check below code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<%@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>W3Adda Page</title> </head> <body> <pre> <form action="second_exception.jsp"> Enter First Number :<input type="text" name="num1" /><br/><br/> Enter Second Number:<input type="text" name="num2" /><br/><br/> <input type="submit" value="Divide"/> </form> </body> </html> |
Output: The above code output will look like below image.
#processingPage.jsp: This page will do process of division and if any exception occurred so exception.jsp page will appear with the help of <%@ page errorPage=”exception.jsp” %> this directive tag. Check below code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<%@page contentType="text/html" pageEncoding="UTF-8"%> <%@ page errorPage="exception.jsp" %> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>W3Adda JSP Page</title> </head> <body> <% String num1 = request.getParameter("num1"); String num2 = request.getParameter("num2"); int a = Integer.parseInt(num1); int b = Integer.parseInt(num2); int c = a / b; out.print("The answer is: " + c); %> </body> </html> |
Output: The output of above code differs according to your input, if any exception occurred so output will be “Error Page” else answer of given input will shown. Check below images.
Example: In this example we will learn that how we can declare our exception page in web.xml file. You do not need to call your page again and again for each jsp, it will automatically called by web.xml. Check below code, exception.jsp and index.jsp page are same as I used in above example, now need to check our processingPage.jsp file and web.xml file.
#web.xml: From here we will declare our exception.jsp page like below code.
1 2 3 4 5 6 7 8 9 10 11 12 |
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <error-page> <exception-type>java.lang.Exception</exception-type> <location>/exception.jsp</location> </error-page> <session-config> <session-timeout> 30 </session-timeout> </session-config> </web-app> |
#processingPage.jsp: Just remove your directive tag from last processingPage.jsp because we have declare our exception.jsp page in web.xml or check below code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<%@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>W3Adda JSP Page</title> </head> <body> <% String num1 = request.getParameter("num1"); String num2 = request.getParameter("num2"); int a = Integer.parseInt(num1); int b = Integer.parseInt(num2); int c = a / b; out.print("The answer is: " + c); %> </body> </html> |
#Output: It will be like our first example because we did not change our exception.jsp page.