How can I catch an exception in JSP files?

I used an ExceptionHandler to throw catch in controllers.

@ExceptionHandler(value = {Exception.class, RuntimeException.class}) public final ModelAndView globalExceptionHandler(final Exception exception) { ModelAndView modelAndView = new ModelAndView("error/500"); modelAndView.addObject("tl_exception", errorSystem.processingError(exception)); return modelAndView; } 

But, for example, if in the jsp file I am tring to get data from a null object, this exception is not cathcing.

I need advice, how can I catch an exception in a jsp file? Or all the errors that I need to catch only in the controllers?

Update:

The best solution is placed in the url for web.xml for errors.

 <error-page> <location>/error</location> </error-page> 

After creating the controller, which is necessary to handle the error from the request:

 @Controller public final class ErrorController { @RequestMapping(value = "/error") public final ModelAndView globalErrorHandle(final HttpServletRequest request) { String page = "error/500"; final String code = request.getAttribute("javax.servlet.error.status_code").toString(); if (null != code && !code.isEmpty()) { final Integer statusCode = Integer.parseInt(code); switch (statusCode) { case 404 : page = "error/404"; case 403 : page = "error/403"; } } return new modelAndView(page); } } 
+4
source share
3 answers

Adding @astrohome to the answer, JSP also gives you the ability to specify an error page for each JSP. Whenever a page throws an exception, the JSP container automatically raises the page with the error.

To set up the error page, use the directive <%@ page errorPage="xxx" %> .

And when handling JSP errors, which you mentioned above, it includes the directive <%@ page isErrorPage="true" %> .

For example, let's say you have the name of the JSP page main.jsp where you are trying to perform an operation on a null object.

main.jsp

 <%@ page errorPage="show-error.jsp" %> <html> <head> <title>Page on which Error Occurs</title> </head> <body> </body> </html> 

show-error.jsp

 <%@ page isErrorPage="true" %> <html> <head> <title>Show Error</title> </head> <body> <p>Exception stack trace:<% exception.printStackTrace(response.getWriter()); %> </p> </body> </html> 
+2
source

There are several options.

First of all, you can use web.xml to catch some specific types (or all of them using java.lang.Throwable ) of exceptions, for example adding this:

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

or

 <error-page> <exception-type>java.lang.ArithmeticException</exception-type> <location>/error.jsp</location> </error-page> 

Another way is to use @ExceptionHandler annotation, please find here

+1
source

If you just want to redirect to the error page, the solution offered by astrohome and Arpit is a good one.

If you really want to catch them and can do something, you have two other ways:

  • filter. Declare a custom filter

     public class exceptFilter implements Filter { ... @Override public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException try { fc.doFilter(req, resp); } catch (Exception ex) { // Exception processing ... } } ... } 

    but be careful, the answer will most likely be complete when you catch the exception ...

  • HandlerExceptionResolver bean. You will find links to it in the Spring Framework Reference Guide - It is more or less similar to the global @ExceptionHandler , but it will act even after the controller action.

+1
source

All Articles