简体   繁体   中英

How I can catch exception in JSP files?

I used ExceptionHandler for catch exception 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 jsp file i am tring to get data from null object, that's exception not cathcing.

I need advice, how i can catch exception in jsp file? Or all error i need catching only in controllers?

Update:

The best solution is put in web.xml uri for errors.

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

After create controller which needed for processing error from 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);
    }
}

Adding to @astrohome answer, JSP also gives you an option to specify Error Page for each JSP. Whenever the page throws an exception, the JSP container automatically invokes the error page.

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

And on Error Handling JSP which you mentioned above include the directive <%@ page isErrorPage="true" %> .

Example, let's say you have a JSP page name main.jsp on which you are trying to perform operation on 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>

There are some options.

First of all, you can use web.xml to catch some specified 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 more here

If you just want to redirect to an error page, the solution proposed by astrohome and Arpit is good.

If you really want to catch them and be able to do anything, you have two other ways:

  1. a 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 beware, the response is likely to have already been commited when you catch the exception...

  2. Implement a HandlerExceptionResolver bean. You will find references for it in Spring Framework Reference Manual - It it more or less like a global @ExceptionHandler , but it will act even before of after controller action.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM