Exception Handling in JSP :
Every application has some error handling code. In web application we normally see that error or exceptions occurred and these exceptions are shown by the tomcat using its default error page. In tomcat default error page, it will show the exception information to the end user, which is not of any use of user. The error page has full stack trace information, which is useful at the developer end but doesn't look good at the production environment.
Many of the developer doesn't deal with Exception handling in jsp, but some of us think about it and try to show simple error page which has user defined error information. The page does not look odd and give appropriate information about error if user wants. It is easy to deal with the error or exceptions, redirect the user at error page if any error or exception occurred in the application. But question is how we will do it.
The answer is hidden in our application's deployment descriptor file web.xml . There is a tag called , using this tag we can redirect user to jsp or servlet when error or exception occured.
For example : suppose we want to redirect user to error.jsp when exception occurred with error code 404, add following lines to web.xml
<error-page>
<error-code>404</error-code>
<location>/error.jsp</location>
</error-page>
We can configure different error pages for different kind of exception codes or we can redirect every exception to single file, it is depends on user.
Now what if we want to redirect user to error.jsp for particular exception ? Here is the answer just put the following code in web.xml
<error-page>
<exception-type>java.io.IOException</exception-type >
<location>/error.jsp</location>
</error-page>
<error-page>
<exception-type>java.lang.NullPointerException</exception-type >
<location>/error.jsp</location>
</error-page>
Suppose if we want to deal with the exception or want any processing before going to error page then we can redirect page to servlet and then to error page. For example
<error-page>
<exception-type>java.io.IOException</exception-type >
<location>/ErrorHandler</location>
</error-page>
Where ErrorHandler is a Servlet name. Now one more important thing is what if we want to redirect all the exceptions to error jsp or any servlet, here is the example below:
<error-page>
<exception-type>java.lang.Throwable</exception-type>
<location>/error.jsp</location>
</error-page>
The super class Throwable will check for the exception and redirect all the runtime and checked exceptions to the error page.
Example of JSP Exception Handling:
index.jsp
<html>
<body>
<h2>User Registration</h2>
<form name="registrationForm" action="CustomerController" method="post">
<table>
<tr><td>
<label for="firstname">First Name</label>
</td>
<td>
<input type="text" name="firstname" id="firstname"/>
</td>
</tr>
<tr><td>
<label for="lastname">Last Name</label>
</td>
<td>
<input type="text" name="lastname" id="lastname"/>
</td>
</tr>
<tr><td>
<label for="address">Address</label>
</td>
<td>
<input type="text" name="address" id="address"/>
</td>
</tr>
<tr><td>
<input type="submit" value="Register"/>
</td>
</tr>
</table>
</form>
</body>
</html>
CustomerController.java
package com.evon.error_handling.customer;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class CustomerController extends HttpServlet {
private static final long serialVersionUID = 1L;
public CustomerController() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String firstname=request.getParameter("firstname");
String lastname=request.getParameter("lastnames");
if(lastname.equals(firstname))System.out.println("First Name and Last Name should be different");
}
}
When we run this application it will show the following user registration page :
Check the CustomerController.java code we will get the java.lang.NullPointerException, because we are getting the wrong parameter value for lastname. So the 'if' condition will throw the NullPointerException. Following screen will shown to the user:
To redirect the user to our simple clean error page we just need to create the error.jsp and set the isErrorPage = true that's it.
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" isErrorPage="true"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Error</title>
</head>
<body>
<p style="color:red">
Error occured in the application. Please contact to administrator.
</p>
</body>
</html>
and put the error-page tag in web.xml
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name> Web Application</display-name>
<servlet>
<servlet-name>CustomerController</servlet-name>
<display-name>CustomerController</display-name>
<servlet-class>com.evon.error_handling.customer.CustomerController</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CustomerController</servlet-name>
<url-pattern>/CustomerController</url-pattern>
</servlet-mapping>
<error-page>
<exception-type>java.lang.Throwable</exception-type>
<location>/error.jsp</location>
</error-page>
</web-app>
Now when we run this application again the NullPointerException will occure but this time the error page will be user defined, as below
User can decorate it in their own way. If user want to show some error or exception information in this error page, then user can get those error information from following request parameters
- request.getAttribute("javax.servlet.error.exception"): this will give the Exception class object,
- request.getAttribute("javax.servlet.error.status_code"): this will give the error code.
- request.getAttribute("javax.servlet.error.servlet_name"): this will return the servlet name where exception has been occurred.
- request.getAttribute("javax.servlet.error.request_uri"): this will return the servlet url shown in the browser's address bar.
0 Comment(s)