25 - JSP Exception Handling

25.1 Overview of JSP Exception Handling

Handling of any exception that may occur in the application is known as Exception Handling. In this chapter we will discuss the possible ways to handle exceptions.

25.2 Ways to handle Exceptions in JSP

a) try/catch blocksThis is the traditional approach of using try- catch blocks in JSP as we normally do in java programs. We can have a code that can potentially raise an exception in try block and have a exception handling code in catch block .

Whenever an exception is occurred , control of program goes in catch block.

In JSP ,we can have a try catch block using scriptlets

<%
  try
  {
  }
  catch(Exception e)
  {
  }
%>

b) isErrorPage and errorPage directive

The errorPage attribute is used to set the error page for the JSP.If the JSP throws exception, the request will be redirected to the page defined in this attribute.

Syntax of errorPage attribute is

    <%@ page errorPage="jsp page name" %>

The isErrorPage attribute is used to inform container that declaring JSP page is an error page. Once declaring any jsp page as error page , container provides an implicit variable “exception” which can be used to get the exception details.

This attribute takes boolean value (true or false ) and the default value is false .

Syntax of isErrorPage attribute is

- <%@ page isErrorPage="true" %>
- <%@ page isErrorPage="false" %>

Note: container will provide exception implicit variable only if page is declared as error page which means all pages does not have exception implicit variable.

To see it working ,

a) Lets create a jsp named error.jsp in WebContent directory with below content

<html>
  <head>
   <title> Error JSP </title>
  </head>
  <body>
    <h4> Something has gone wrong </h4>
  </body>
</html>

b) Create another jsp testErrorPage.jsp with below content. This page will generate null pointer exception as we are calling toString() method on null String.

<html>
  <head>
   <title> Test Error Page JSP </title>
  </head>
  <body>
    This page is generating error intentionally.
        <%
        String str = null;
        out.println(str.toString());
        %>
  </body>
</html>

c) Access testErrorPage.jsp using http://localhost:8080/jsp-tutorial/testErrorPage.jsp , you will see below screen

d) Now add errorPage attribute ( <%@ page errorPage="error.jsp" %> ) in testErrorPage.jsp and access it again. This time page will be redirected to error.jsp (see page content and Url in below figure)

Now Change the error.jsp as shown below and access testErrorPage.jsp again

<%@ page isErrorPage="true" %> 
<html>
  <head>
    <title> Error JSP </title>
  </head>
  <body>
    <h4> Something has gone wrong </h4>
    <h5>This page is declared as isErrorPage so we have exception implicit variable</h5>
    <%
    exception.printStackTrace(new java.io.PrintWriter(out));
    %>
  </body>
</html>

c) Declaring Error Pages in deployment descriptor

Deployment descriptor (web.xml) provides a way to configure error pages based on exception types or error codes.

Syntax for the error-page tags are –

<error-page>
  <exception-type>java.lang.Throwable</exception-type>   - Any child of Throwable is allowed
  <location>/error.jsp</location>                              - jsp page to be displayed on this exception
</error-page>
<error-page>
  <error-code>404</error-code> - error code
  <location>/error.jsp</location> -- jsp page to be displayed on this exception
</error-page>

To see these tags in action –

a) Create an nullPointerError.jsp this jsp will be displayed for null pointer exceptions

b) Create an pageNotFound.jsp which will be displayed for error codes

c) Create an testError.jsp - which will raise exceptions

A) Add below in web.xml

<error-page>
  <exception-type>java.lang.NullPointerException</exception-type>
  <location>/nullPointerError.jsp</location>
  </error-page>
  <error-page>
  <error-code>404</error-code>
  <location>/pageNotFound.jsp</location>
  </error-page>

B) nullPointerError.jsp

<html>
  <head>
  </head>
  <%@page isErrorPage="true" %>
  <body>
    <h4>==== Null Pointer  Error   === </h4>
    <%=
    exception.getMessage()
    %>
  </body>
</html>

C) pageNotFound.jsp

<html>
  <head>
  </head>
  <%@page isErrorPage="true" %>
  <body>
    <h4>==== Page Not Found    === </h4>
    Requested Resource does not exist !!
  </body>
</html>

D) testError.jsp

<html>
  <head>
   <title> Test Error Page JSP </title>
  </head>
  <body>
    This page is generating error intentionally.
        <%
        String type = request.getParameter("type");
        if(type.equals("error"))
        {
            String str = null;
            out.println(str.toString());
        }
        else 
        {
        response.setStatus(404);
        }
        %>
  </body>
</html>

Access testError.jsp using http://localhost:8080/jsp-tutorial/testError.jsp?type=error

Access testError.jsp using http://localhost:8080/jsp-tutorial/xyz.jsp

Like us on Facebook