JSP Exception Handling

JSP Exception Handling

The procedure of handling exceptions that can be occurred in the application is called exception handling. Exception handling in JSP uses the same class objects of exception as in Java but in JSP exception handling is much easier. In our application we cannot prevent all the error; there is always some error so it’s a good practice to make an error response page.

 

Ways to perform exception handling in JSP:

There are three different ways in JSP to perform exception handling:

  1. By using the isErrorPage and the errorPage page directive attributes.
  2. By using the <error-page> tag in DD that is deployment descriptor.
  3. By using the try… catch block.

 

Example of isErrorPage and errorPage attribute:

The isErrorPage attribute of the page directive is used as it appoints the JSP page as an error page which handles exceptions. Consider the following example in which we have a JSP page and an error page to handle exceptions:

handlingError.jsp:

<%@ page isErrorPage = “true” %>

< html >

< body >

< strong >  Error! Can’t reach the site you requested. </ strong >

< /body >

< /html >

asd.jsp:

<% @ page errorPage = “handlingError.jsp” %>

< html >

< body >

<% int y = 5/0; %>

< /body >

< /html >

It should be noted that the errorPage attribute is used to inform the web container if there is any exception occurred in the page then the web container should forward the request to the specified error page. In the above example when an error occurs in the page asd.jsp the web container forwards the request to the handlingError page and the user is directed to that page where a message is printed telling the user that an error has been occurred.

 

Declaring error page in the deployment descriptor:

The error pages can also be declared for the whole web application in the deployment descriptor by using the <error-page> tag.

Consider the following lines of code in which we have declared an error page for all the types of exception:

< error-page >

< exception-type >java. lang. Throwable </ exception-type >

< location >/ handlingError.jsp < / location >

</ error-page >

Now consider the following lines of code in which we have declared for detailed exception:

< error-page >

< exception-type >java. lang. ArithmeticException </ exception-type >

<location >/ handlingError.jsp < / location >

</ error-page >

Consider the following lines of code in which we have declared an error page for that is based on the HTTP status code:

< error-page >

< error-code > 404 < /error-code >

< location >/ handlingError.jsp </location >

< / error-page >

 

Using the try…catch block:

The try…catch block in JSP page is similar to the try…catch block used in java programming language. Consider the following example in which try…catch block is used in the JSP page:

< html >

< head >

< title > Using Try…Catch < / title >

< / head >

< body >

<%try{

      int x = 10;

      x = x / 0;

      out. println (“Answer: ” + x);

   }

catch (Exception e){

out. println (“Exception occurred! ” + e.getMessage());

}

%>

</body>

</html>