Boon Nam GOH wrote:
>
> I have read the following warning in several books that for setHeader, need
> to:
> "Be sure to set response headers before sending any document content to
> the client."
>
> Eg. of such a warning - see page 144 of:
> http://developer.java.sun.com/developer/Books/cservletsjsp/chapter7.pdf
>
> There is a sample of do proper setHeader for Servlets (see page 156, 157 in
> the above PDF).
>
> Basically the setHeader command is placed BEFORE the PrinWriter command.
> ie.
> response.setHeader("Refresh", "5");
> response.setContentType("text/html");
> PrintWriter out = response.getWriter();

The reason for this is that in an HTTP response, all headers are sent
as the first part, followed by a body. Ignoring buffering for a moment,
this basically means that as soon as you start writing the body of the
response, all headers must be sent to the client.

Typically, an HttpServletResponse implementation buffers the output
so you may be able to write a number of bytes before the headers are
sent, but it's still a good idea to take care of all headers before
you start writing the body.

> How can I achieve this in JSP?
>
> I have tried using
> <% response.setHeader("Cache-Control", "no-cache");%>
> <HTML>
>   ....
>
> but I find that the generated servlet code is as follows:
> out = pageContext.getOut();
> out.print(_jspx_html_data[0]);
> response.setHeader("Cache-Control", "no-cache");
>
> ie. the setHeader comes AFTER some print commands.
>
> Is this acceptable? or is there something wrong with the way I am coding my
> setHeader in the JSP?
>
> [I know that in the case of setContentType, there is a special command
> <%@ page contentType="...." %> to ensure that the setContentType command is
> generated in the correct position - ie. before any out.print command.
> Is there something equivalent for setHeader?]

The output from a JSP page is always buffered by the container, so it's
typically less of an issue if you write some body content before you
write the body. The default buffer is 8 kb, and you can increase it with
the page directive if that's not enough:

  <%@ page buffer="32kb" %>

You can also control what should happen when the buffer is full: flush it,
after first sending the headers, and continue, or throw an exception. This
is also specified with the page directive:

  <%@ page autoFlush="false" %>

Default is true, meaning "flush and continue".

Another thing to be aware of is the in JSP 1.1, the <jsp:include> action
always flushes the buffer, so all headers must be set before this action
is used.

For more about buffering, and a lot of other stuff, you may want to read
my JSP book:

  <http://TheJSPBook.com/>

Hans
--
Hans Bergsten           [EMAIL PROTECTED]
Gefion Software         http://www.gefionsoftware.com
Author of JavaServer Pages (O'Reilly), http://TheJSPBook.com

===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
For digest: mailto [EMAIL PROTECTED] with body: "set JSP-INTEREST DIGEST".
Some relevant FAQs on JSP/Servlets can be found at:

 http://java.sun.com/products/jsp/faq.html
 http://www.esperanto.org.nz/jsp/jspfaq.html
 http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
 http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets

Reply via email to