Pradyut Bhattacharya
Pradyut Bhattacharya

Reputation: 5748

java filter forward throwing illegalstateexception

i m intercepting the request url and forwarding that to another url

like

some_application/image_20.jpeg to some_application/image_345.jpeg

I am doing this using filters.

now my code is:

   public void doFilter(ServletRequest request, ServletResponse response,
                     FilterChain chain)
    throws IOException, ServletException {
    //some code

    RequestDispatcher request_Dispatcher=request.getRequestDispatcher(forward_url);
    request_Dispatcher.forward(request,response);



 Throwable problem = null;
 try {
 chain.doFilter(request, response);
 }

    catch(IllegalStateException ise)
    {

    }
    catch(Throwable t) {

 problem = t;
 t.printStackTrace();
}

  }
}

as i m using RequestDispatcher to forward requests.. and its working correctly

is it normal for this code to throw IllegalStateException

as i catch it in my code and leave it not to throw any messages..

now i m worried if it can harm the overall container or slow the performance

or i can change some code and not get any IllegalStateException

thanks

Upvotes: 0

Views: 2201

Answers (1)

McDowell
McDowell

Reputation: 108979

request_Dispatcher.forward(request,response);
chain.doFilter(request, response);

You cannot do both these things. By the time you pass the request along the chain to the end resource, you have already committed a response via forward.

Upvotes: 5

Related Questions