Friday 31 October 2014

Servlet Error Handling

Servlet application will not be perfect until it works on all negative or positive scenario.So, Error handling is also required during servlet programming. In this chapter I will explain about how to handle servlet errors.

Whenever servlet will throw any error by any reason whether it is because of resource not found or authentication issue, etc...then error stack-trace will be taken by web container and converted to html and simply send by response to render to the browser. So, end user will not be having understanding  about stack-trace.

Servlet container provides way to handle any kind of error and sends specific response or it can redirect to other page in case of error happen. So, rather than sending stack-trace as it is to the end user, we can navigate user to some other meaningful page so, that end user easily can understand that something went wrong.But to do this container should aware about this i.e. for which error what action should take or which page should redirect.

Hence to tell to servlet container we need to configure the deployment descriptor (web.xml) i.e. for which error code or which exeception type, which servlet should called.

Web.xml

 <error-page>
    <error-code>404</error-code>
    <location>/ExceptionHandler</location>
  </error-page>
   
  <error-page>
    <exception-type>javax.servlet.ServletException</exception-type>
    <location>/ExceptionHandler</location>
 </error-page>

error-page tag is used to handle error in servlet. It can either handled for error code or for exception type and location inner tag is used to tell about servlet location.In above example whenever error code 404 will arise or if ServletException will arise then ExceptionHandler Servlet will be invoked.
So, we can say any error can be handled by servlet.

Before servlet container invokes the servlet to handle the exception, it sets some attributes in the request to get useful information about the exception, some of them are javax.servlet.error.exception, javax.servlet.error.status_code, javax.servlet.error.servlet_name and javax.servlet.error.request_uri

For Example Below is given ExceptionHandler shows how to handle the specific error



protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        Throwable throwable = (Throwable) request
                .getAttribute("javax.servlet.error.exception");
        Integer statusCode = (Integer) request
                .getAttribute("javax.servlet.error.status_code");
        String servletName = (String) request
                .getAttribute("javax.servlet.error.servlet_name");
        String requestUri = (String) request
                .getAttribute("javax.servlet.error.request_uri");
    
        PrintWriter out = response.getWriter();
          out.write("<html><head><title>Error Details</title></head><body>");
           
          if(statusCode != 500){
              out.write("Status Code:"+statusCode+"<br>");
              out.write("Requested URI:"+requestUri);
          }else{
              out.write("<ul><li>Servlet Name:"+servletName+"</li>");
              out.write("<li>Exception Name:"+throwable.getClass().getName()+"</li>");
              out.write("<li>Requested URI:"+requestUri+"</li>");
              out.write("<li>Exception Message:"+throwable.getMessage()+"</li>");
              out.write("</ul>");
          }
           
          out.write("<br><br>");
          out.write("</body></html>");
}

If there are multiple error-page entries, let’s say one for Throwable and one for IOException and application throws FileNotFoundException then it will be handled by error handler of IOException.
You can also use JSP page as exception handler, just provide the location of jsp file rather than servlet mapping.


Thanks!!!!

No comments:

Post a Comment