Exception Handling in JSP

Exception Handling is a process of handling exceptional condition that might occur in your application. Exception Handling in JSP is much easier than Java Technology exception handling. Although JSP Technology also uses the same exception class object.There are two ways to perform exception handling in JSP.

  1. By errorPage and isErrorPage attributes of page directive
  2. By error-page element in web.xml file


Example of isErrorPage and errorPage attribute :

We can handle the exceptions in jsp by specifying errorPage in the page directive, <%@ page errorPage = "some jsp">. If any exception will be thrown then the control to handle the exception will be passed to that error page where we can display information to the user about what's the reason behind throwing the exception.

Exception Handling is a process of handling exceptional condition that might occur in your application. Exception Handling in JSP is much easier than Java Technology exception handling. Although JSP Technology also uses the same exception class object.There are two ways to perform exception handling in JSP.

  1. By errorPage and isErrorPage attributes of page directive
  2. By error-page element in web.xml file


Example of isErrorPage and errorPage attribute :

We can handle the exceptions in jsp by specifying errorPage in the page directive, <%@ page errorPage = "some jsp">. If any exception will be thrown then the control to handle the exception will be passed to that error page where we can display information to the user about what's the reason behind throwing the exception.


When we are writing java code inside the Jsp, we may get exception inside that.In order to handle the exception we need to write try/catch blocks many times in all the Jsp pages.To avoid the try ,catch block and to centralize the exception handling use errorPage and IsErrorPage attribute inside the JSP where you want to handle all the exceptions.

For centralizing the exception handling enable the exception implicit objects in the JSP pages for which we want to handle the exception by writing <%@ page errorPage = "some jsp"> page directive.

Enable the exception implicit objects in the JSP pages from which we want to handle the exception by writing <%@ page isErrorPage="true"%> page directive.

Let us consider the image given below.

JSP Exception Handling

In JSP you can specify Error Page for each JSP. Whenever the page throws an exception, the JSP container automatically invokes the error page.

Following is an example to specifiy an error page for a index.jsp. To set up an error page, use the <%@ page errorPage="some jsp" %> directive.

index.jsp

<%@ page errorPage="ShowError.jsp" %>
<html>
<head>
   <title>Error Handling Example</title>
</head>
<body>
<%
   // Throw an exception to invoke the error page
   boolean x = true;
   if (x)
   {
      throw new RuntimeException("Error Occured!!!");
   }
%>
</body>
</html>

Now you would have to write one Error Handling JSP errorpage.jsp, which is given below. Notice that the error-handling page includes the directive <%@ page isErrorPage="true" %>. This directive causes the JSP compiler to generate the exception instance variable.

errorpage.jsp

<%@ page isErrorPage="true" %>
<html>
<head>
<title>Error Page</title>
</head>
<body>
<h1>oops...</h1>
<p>Sorry, an error occurred.</p>
<p>Here is the exception stack trace: </p>
<% exception.printStackTrace(response.getWriter()); %>
</body>
</html>

Output in Browser :

JSP Exception Handling


Download this Example(Developed in Eclipse)


Handling exceptions using error-page element in the 'web.xml' file :

This approach is better as the user doesn't need to use the page directive to declare the error page in each jsp file.Making a single entry in the web.xml file serves the purpose. We can either specify the exception type or the error code with the location element. If we want to handle all the exceptions we need to specify the java.lang.Exception in the exception type element. The web.xml file for each of these cases is given as under :


1) web.xml file if you want to handle any exception :

<web-app>   
 <error-page>  
  <exception-type>java.lang.Exception</exception-type>  
  <location>/error.jsp</location>  
  </error-page>     
</web-app>

This approach is better if you want to handle any exception. If you know any specific error code and you want to handle that exception, specify the error-code element instead of exception-type as given below:

2) web.xml file if you want to handle the exception for a specific error code :

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 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_2_5.xsd">
    <error-page>
        <error-code> 500 </error-code>
        <location> /error.jsp </location>
    </error-page>
    <welcome-file-list>
        <welcome-file> index.jsp </welcome-file>
    </welcome-file-list>
</web-app>


comments powered by Disqus