How to redirect errors to an page when an exception occurs from a servlet?

I am writing a servlet, in case any exception occurs, I do not want to display an error / error message in the browser, so I am redirected to the configured error page. So I did the following:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try{ //Here is all code stuff }catch(Exception e){ request.getRequestDispatcher("/ErrorPage.jsp").forward(request, response); e1.printStackTrace(); } 

Is this correct, if I am wrong, please correct me, and if there is any better mechanism, please tell me.

+5
source share
3 answers

The only way to handle this in the general case is to use web.xml , as shown below:

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

Servlet throws ServletException and IOException , but if you want to handle runtime exceptions and all other exceptions in one exception handler, you can specify the type of exception as Throwable . You can use multiple error page entries that will handle different types of exceptions and have different handlers.

Example:

 @WebServlet("/ErrorHandler") public class ErrorHandler extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processError(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processError(request, response); } private void processError(HttpServletRequest request, HttpServletResponse response) throws IOException { //customize error message 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"); if (servletName == null) { servletName = "Unknown"; } String requestUri = (String) request .getAttribute("javax.servlet.error.request_uri"); if (requestUri == null) { requestUri = "Unknown"; } request.setAttribute("error", "Servlet " + servletName + " has thrown an exception " + throwable.getClass().getName() + " : " + throwable.getMessage()); request.getRequestDispatcher("/ErrorPage.jsp").forward(request, response); } } 
+1
source

In some method, you will have the following:

 try { // something } catch (Exception e) { sendErrorRedirect(req, res, "/errorpage.jsp", e); } // then.... sendErrorRedirect looks like this: protected void sendErrorRedirect(HttpServletRequest request, HttpServletResponse response, String errorPageURL, Throwable e) { try { request.setAttribute ("javax.servlet.jsp.jspException", e); getServletConfig().getServletContext().getRequestDispatcher(errorPageURL).forward(request, response); } catch (Exception ex) { putError("serXXXXX.sendErrorRedirect ", ex); } } 
+1
source

One way to handle it in the general case is to use web.xml, as shown below:

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

I just included an IO exception, but you could say that with SQLException you could add another error page and another place for it. Similarly, you can specify the type java.lang.Exception and one handler that handles everything.

+1
source

Source: https://habr.com/ru/post/1206415/


All Articles