Thursday, December 22, 2016

How to redirect to error page when exception occurs from servlet?

Servlet is thrown ServletException and IOException. If you want to handle each exception seperately then you can handle multiple handler in web.xml file.

For example: you've to use exception-type or error-code and location in error-page to handle specific exception or error code.



        <error-page>
           
<exception-type>java.io.IOException</exception-type>                           
            <location>/Handler1</location>        
        </error-page>
   



       
<error-page>         
                 <exception-type>javax.servlet.ServletException</exception-type>         
                 <location>/Handler2</location>        
         </error-page>
   




        
<error-page>          
               <error-code>404</error-code>          
               <location>/invalidRequest</location>        
          </error-page>
   


If you want to handle runtime exceptions and all other exceptions in a single exception handler, you can provide exception-type as Throwable.

Only way to handle it in a generic way is to use web.xml like below:


        <error-page> 

                      <exception-type>java.lang.Throwable</exception-type>                      
                      <location>/Handler</location>        
        </error-page>   

Example:

We handle the error in /Handler  servlet and forward / redirect the error to errorPage.jsp.

/Handler:

@WebServlet("/Handler")
public class ExceptionHandler extends HttpServlet{

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Process(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Process(req, resp);
    }
   
    public void Process(HttpServletRequest req, HttpServletResponse resp)  throws ServletException, IOException
    {
        Throwable throwable = (Throwable) req.getAttribute("javax.servlet.error.exception");
        int status_code = (Integer) req.getAttribute("javax.servlet.error.status_code");
        String servlet_name = (String) req.getAttribute(" javax.servlet.error.servlet_name");
       
        if(servlet_name==null)
        {
            servlet_name = "unknown";
        }
        String uri = (String) req.getAttribute("javax.servlet.error.request_uri");  
       
        if(uri==null)
        {
            uri = "unknown";
        }
       
        req.setAttribute("error", "Servlet: " + servlet_name + " has thrown an exception "
                + throwable.getClass().getName() + ": " + throwable.getMessage());
        req.getRequestDispatcher("/errorPage.jsp").forward(req, resp);
    }
   

errorPage.jsp:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Error Occurred</title>
    </head>
    <body>
        <h1>${error}</h1>
    </body>
</html>




No comments:

Post a Comment