问题
I had a weird problem today. I have a head.jsp ( ending with out.flush ) which is being included by other pages. most of the pages doesn't have problem with this out.flush(); but one of my page is failing with IllegalStateException on jboss 4.2.2 GA.
I checked the related java file ResponseFacade.java I think. And see that problem occurs because of if (isCommitted) check.
I just removed the include statement from my problemmatic page and it is working now. But the question is; why the other pages does not have problem with this page while the only one page has ?
or if a jsp file is being included by other pages. do I need to out.flush() in this included pages ?
回答1:
Your web container will of course flush the response's output stream when it needs to. You don't need any call to flush. BTW, Java code in JSP is bad practice anyway.
回答2:
I don't have the same setup but had a similar problem.
With <% out.flush(); %>
in my jsp, <jsp:include page="abc.jsp" />
wasn't working any more. The specified page was not included.
I solved it by using <%@ include file="abc.jsp" %>
Hope this helps.
回答3:
When you flush() you send the content of the buffer to the client and then empty the buffer. There is no bad practice in that, you can flush as many times as you need.
Just remember that the 1st flush() of a JSP page will send also the HTTP HEADERs, and the buffer is marked as committed (status -> isCommitted). Meaning: at this point you can no more send HTTP HEADERs (like eg page_forward, cookies).
Chances are that you are trying to send some HTTP HEADER in the page after the flush() has been done (after the include).
回答4:
This is the only method that worked for me (JSP over Tomcat/Apache).
Main.jsp
<jsp:include page="flush.jsp" flush="true" />
Flush.jsp (blank)
<%
%>
来源:https://stackoverflow.com/questions/8680172/jsp-out-flush-issue