wood burning stoves 2.0*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes flush - jsp:include Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "flush - jsp:include" Watch "flush - jsp:include" New topic
Author

flush - jsp:include

Elias N Varghese
Greenhorn

Joined: Mar 05, 2006
Posts: 14
<jsp:include page="B.jsp" flush="true"/>


can anyone explain this flush attribute?what it is used for?


Thanks.


I was born intelligent - education ruined me.
Marc Peabody
pie sneak
Sheriff

Joined: Feb 05, 2003
Posts: 4727

You'll see that attribute a lot because it used to be mandatory. In fact, at one time it was also mandatory that the value be true.

JSPs are buffered by default. This means that as stuff is written to the JSPWriter (more familiarly known as out), each character does not immediately get sent to the client through the response. The characters get stored up in a buffer. The concept is similar to something else that flushes.

When the JSPWriter (aka out) is flushed, everything in the buffer gets sent to the client. You can flush as many times as you need to until you close the response. You can put the following in a JSP and it works just fine:
<%
out.write("This will print!<br/>");
out.flush();
out.write("This will also print!<br/>");
out.flush();
out.write("This will get sent to the client too!<br/>");
%>

But there's a gotcha you need to know for the exam that's explained on page 205 of HFSJ - after you've flushed once, you've committed your response and you can't change your mind and instead forward the request!

So, back to your question. Declaring flush="true" in the include will simply flush out your buffer before running the include. It's pretty rare that including it would ever make much of a difference so most people nowadays leave it off so no one asks them, "What's that do?"
[ May 24, 2006: Message edited by: Marc Peabody ]

A good workman is known by his tools.
Adam Czysciak
Ranch Hand

Joined: Feb 25, 2005
Posts: 82
Originally posted by Marc Peabody:

But there's a gotcha you need to know for the exam that's explained on page 205 of HFSJ - after you've flushed once, you've committed your response and you can't change your mind and instead forward the request!


BTW - I also wondered about one situation. What if we don't call out.flush() before <jsp:forward.../>, but there will be so much data that buffer will flush itself? This is quite common approach for automatic buffers flushing, do we have any guarantee in JSP that buffer's won't be flushed until we explicitly call out.flush()? In other words - is JSPWriter's buffer limited?


Adam<br /> <br /><i>SCJA, SCJP 5.0, SCWCD 1.4, SCBCD 5, SCEA 5</i>
Marc Peabody
pie sneak
Sheriff

Joined: Feb 05, 2003
Posts: 4727

From the JSP spec on the buffer attribute of the page directive:
Specifies the buffering model for the initial out JspWriter to
handle content output from the page.
If none, then there is no buffering and all output is written
directly through to the ServletResponse PrintWriter.
The size can only be specified in kilobytes. The suffix kb is
mandatory or a translation error must occur.
If a buffer size is specified then output is buffered with a
buffer size not less than that specified.
Depending upon the value of the autoFlush attribute, the
contents of this buffer is either automatically flushed, or an
exception is raised, when overflow would occur.
The default is buffered with an implementation buffer size of
not less than 8kb.


and for the autoflush attribute of page directive:
Specifies whether the buffered output should be flushed
automatically (true value) when the buffer is filled, or
whether an exception should be raised (false value) to
indicate buffer overflow. It is illegal, resulting in a translation
error, to set autoFlush to false when buffer=none. The default
value is true.


So, by default the buffer exists and must be at least 8kb in capacity with the behavior that all approaches to the size limit will flush the buffer automatically. You can change the above values for different behavior.
Marc Peabody
pie sneak
Sheriff

Joined: Feb 05, 2003
Posts: 4727

And from Sun:
Be careful when using <jsp:forward> with unbuffered output. If you have used the <%@ page %> directive with buffer=none to specify that the output of your JSP file should not be buffered, and if the JSP file has any data in the out object, using <jsp:forward> will cause an IllegalStateException.


This implies, in my opinion, that a forced auto-flushing of a buffer followed by a jsp:forward would also create an IllegalStateException. Practically speaking though, I doubt you'd ever have a jsp:forward so far down on a jsp that you'd need to worry about that. The jsp:forward tags are usually pretty darn close to the top.
[ May 24, 2006: Message edited by: Marc Peabody ]
 
jQuery in Action, 2nd edition
 
subject: flush - jsp:include