Heinz Wehner wrote:

> Nic,
>
> thanks for your answer that explains why getSession must be called
> before writing any output to the response. However, it's still not
> clear why you must even do this before sending any output in case
> you respond using a writer. My guess would be that by accessing a
> Writer the servlet container already writes the response header.
>

That is not correct.  Neither response.getWriter() or
response.getOutputStream() necessarily triggers the response headers being
sent.  What matters is how buffering of the response data works in your
servlet container.  To improve efficiency, most servlet containers buffer the
response output.  Prior to the 2.2 spec the buffer configuration was in the
hands of the servlet container; with 2.2 you now get to control it, as well as
being able to reset the output buffer (if nothing has yet been sent), and tell
whether any output has been sent yet.

As long as the output has not yet been committed, you can still add HTTP
headers (which is how the cookie that is used for session state maintenenace
is actually sent to the browser).

> Is that correct? I'm also uncertain as to wether this behaviour
> conforms with the Servlet API specification.
>

If I'm using sessions, I always make it a habit to call request.getSession()
at the very beginning of my doGet() or doPost() method.  This protects me from
any cases where the buffer might be smaller than I expect, or against me
moving code around and putting output-generating code earlier than it used to
be (suddenly causing sessions not to work).

In JSP pages, this happens for you automatically, because the generated code
does a request.getSession() call at the beginning of the service method --
unless you've explicitly indicated that this page does not particpate in
sessions.

>
> Heinz Wehner
> (Karlsruhe, Germany)
>

Craig McClanahan

___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".

Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html

Reply via email to