I have a requirement to forward a request a third party link from my servlet.
For this i m using response.sendRedirect("http://google.com") in my servlet. But this is sometimes failing in some particular cases without any errors but giving a blank page.
So i thought to probably use
RequestDispatcher dispatcher =
if (dispatcher != null) dispatcher.forward(request, response);
return; but the above is giving me the following error
11:07:08,318 ERROR [ContainerBase] Servlet.service() for servlet jsp threw exceptio
java.lang.IllegalStateException: Cannot forward after response has been committed
1. You cannot use RequestDispatcher to forward a request outside the Servlet context of the application.
2. IllegalStateException is thrown when you send something as a response before forwarding the request. See if you are calling response.getWriter/getOutputStream to send some response before forwarding.
3. Check the server logs, if you see a blank page when you sendRedirect, there might be an exception on the server causing the problem...
which basically has the following post
Basically you are doing too much output before you decide to you need to forward to a new page.
Output could just be the plain HTML text in the jsp document - that all gets converted to out.println();
Basically if it writes more than your buffer (default is 8kb) then the buffer is flushed, and you can't forward/redirect anymore as you are committed to complete the current response.
There are two ways of handling this issue
1 - shift your forward/redirect code, so that it happens as early as possible. BEFORE any HTML is output.
2 - increase the buffer size. <%@ page buffer="16kb"%> for instance.
This means it can write out more text before you decide to forward.
That might stop the error on your page.
3 - Oh, and if you have set your buffer to zero, or you have directly called flush() somewhere in your code, that would be the cause. Don't do those things :-)
Can someone tell me does it have a performance impact if we increase or decrease page buffer
Generally requests go to the JSP through a servlet and you should decide in the servlet whether to sendRedirect or not. I would not recommend redirecting or forwarding a request from a JSP. And Mohammed, you've set autoFlush to true, so the buffer will be flushed automatically, setting autoFlush to false does what you said...