<jsp:include>and then response.sendRedirect
Hii
In jsp after u include any page by <jsp:include> directive and then try to do response.sendRedirect("somepage.jsp");
This does not work.
But if i use response.sendRedirect("somepage.jsp"); before <jsp:include>
then it works perfectly fine.
Why this is so?
and What is solution to this problem?
When you include another resource from within a jsp, the container by default flushes the output stream. This stream would contain any output that's written by the first resource.
Now when you try to redirect, you get an error because redirect cannot happen after any output has been flushed.
ram.
You can not do a 'sendRedirect' after the page has been 'committed'. A page becomes committed when data is sent to the user (as opposed to being stored on a local buffer on the server). This is because the sendRedirect command actually sends a response to the client that they should go to another page - something that can't be done if the client already has part of this page displayed.
Anyway, a jsp:include will often flush the buffer so that any data currently written in the JSP gets sent to the client (thus committing the response). The first thing to try would be to do:
<jsp:include page="..." flush="false"/>
But I imagine this won't help much either, since, chances are, you are putting enough data in the buffer to fill it and force it to flush anyway. Your option then would be to increase the size of your buffer. Put this at the top of the JSP page:
<%@ page buffer="64kb" %>
This sets the buffer to 64kb, much more than the default 8kb.
The best option, though, would be to try to move all the logic that would dictate your need to do a sendRedirect at the beginning of the JSP - before you do any display, and before you do the include. Then based on some condition do the the sendRedirect or do the display...
Thank U for ur response.
But I dried every thing what U said. But its not working.
And my include page is Header page and I am putting connection code header and getting connection object from session so this include page comming right middle of my jsp page as i am also passing userid to it as parameters.
So I can not move it down.
And I reqire response.sendRedirect for Errors as well as acknowledgement pages.
So I did redirect befor jsp:include and Errors after that are send via upper <%@ page errorPage="Error.jsp">
and now it is working fine.
But thir has to be some solution for sendRedirect after jsp:include.
Thanks.
> But thir has to be some solution for sendRedirect
> after jsp:include.
> Thanks.
You are incorrect. There doesn't need to be (other than the options previously mentioned). If you send data to the client, the client can't be asked to make a new request to another page. You have to not send data to the cliend.
The only other option would be use JavaScript to send send the page to a new address. Note that JS is client side, may be turned off (and thus not work on all user machines), and can be manipulated by clients (making it work incorrectly or not at all).