How to handle exceptions from the servlet they create?

I have a simple (Servlet, JSP and JSTL) web application whose main functionality is to display images received from the internal server. The controller servlet sends the user to the JSP, which in turn uses a different servlet to display the resulting image on the same JSP. Further, JSP has a line similar to:

<a href="<c:out value='${imageURL}'/>"><img src="<c:out value='${imageURL}'/>" alt="image view" border="1"></a> 

which raises a GET request on the servlet that generates the image, causing it to generate the image.

My question is: how do I handle the exceptions thrown by this image generation servlet?

I already have an error page defined (in web.xml) for handling ServletException in my web application, but this does not work for this servlet generating the image and leads to the following errors appearing in my Tomcat server logs:

 SEVERE: Exception Processing ErrorPage[exceptionType=javax.servlet.ServletException, location=/WEB-INF/ExceptionPage.jsp] java.lang.IllegalStateException: Cannot reset buffer after response has been committed 

What is my appeal in this situation?

I would like to be able to handle the Exceptions thrown from this servlet generating the image and display some error in the main user interface or redirect the user to another error page.

+4
source share
5 answers

You cannot change the answer to redirect to the error page , but send the answer. Then it’s too late to change the whole answer. You cannot request already sent bytes from the client side. What IllegalStateException means here. This is the point of no return.

The best you can do is simply to register an exception or rewrite the code so that it does not write a single bit of the response (also without setting the response headers), while the business logic has not completed its task yet. Once you determine that the business logic did not raise any exceptions, start writing (and thereby also indirectly commit) the answer. If business logic has raised an exception while the answer has not yet been touched, you can simply throw it safely so that it gets to the error page. Although in the case of the image servlet, you can also pass the standard 404.gif response instead. This is because you cannot display another HTML page (error) in the <img> element, and you also cannot change the URL of the parent JSP / HTML page, as it concerns another request.

+3
source

According to the servlet API, the servlet should not call getWriter () and getOutputStream () on the same response object, since it throws an IllegalStateException. This is usually the source of this exception. If you output binary data and an image file, you should use getOutputStream ().

+1
source

It looks like you have the problem in your ExceptionPage.jsp, and not in your servlet code.

And this one

java.lang.IllegalStateException: Cannot reset buffer after response has been committed

Means that you have already tried to send a response. Perhaps you opened the output stream directly and wrote some data to it. Once you do this, you cannot try to set headers, etc. To the answer (they are already on their way to the client).

You need to improve government. The best way to do this is to separate the request preprocessing from the response generation. When you write an answer, you can only die or die. To do this, make sure that you do not catch IOExceptions from the output of the response, wrap them in a ServletException and redirect them to the error page. You really cannot handle them in the context of the current request.

+1
source

First, indicate why the Illegal State exception is thrown. Instead of dealing with the thrown exception, you probably just want to fix your code so that it goes away.

0
source

You must catch the exception and redirect the request using RequestDispatcher to the desired page:

  public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // The following piece of code results in NumberFormatException which will // be detected by the container. The RequestDispatcher object will forward // the same request to the other resource, here the file: forwardedJSP.jsp try { int test = Integer.parseInt("abc"); } catch (NumberFormatException nfe) { RequestDispatcher rd = request.getRequestDispatcher("/forwardedJSP.jsp"); rd.forward(request, response); }} 
0
source

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


All Articles