tomcat-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Mark Roth <mark.r...@sun.com>
Subject Re: JSP implicit writer (out) vs. response.getWriter()
Date Mon, 24 Nov 2003 16:14:09 GMT
Hi Karl,

The result is not unexpected.  See the JSP 2.0 specification, section 
JSP.1.7:

"...JSP page authors are prohibited from writing directly to either the 
PrintWriter or OutputStream associated with the ServletResponse."

The reason is that writes performed by the JSP page are normally 
buffered.  There is an implicit object 'out' that writes to the buffered 
JspWriter which will accomplish what you are looking for, in the right 
order.  So you should do this instead:

    <html>
    <body>
    Me first.
    <% out.println("Me second"); %>
    </body>
    </html>

Hope this helps.

---
Mark Roth, Java Software
JSP 2.0 Co-Specification Lead
Sun Microsystems, Inc.


Karl Goldstein wrote:
> After thinking about it a bit more, it seems the solution to the issue I raised yesterday
is potentially simple: just use a "wrapped" response for the JSP response variable, which
returns the JspWriter.  
> 
> create a new package class JspResponseWrapper: 
> 
>   class JspResponseWrapper extends HttpServletResponseWrapper {
> 
>     private Writer out;
>     
>     JspResponseWrapper(HttpServletResponse response, JspWriter out) {
>       super(response);
>       this.out = out;
>     }
> 
>     public Writer getWriter() {
>       return out;
>     }
> 
>     ...override other methods related to output stream/writer
>   }
> 
> 
> Then in PageContextImpl._initialize:
> 
> this.response = new JspResponseWriter(response, this.out);
> 
> Thoughts?
> 
> Regards,
> 
> Karl Goldstein
>   ----- Original Message ----- 
>   From: Karl Goldstein 
>   To: tomcat-dev@jakarta.apache.org 
>   Sent: Saturday, November 22, 2003 10:32 PM
>   Subject: JSP implicit writer (out) vs. response.getWriter()
> 
> 
>   Hi all,
> 
>   I recently started working with Tomcat (4.1.29) and ran into an inconsistency between
the implicit "out" variable in a JSP and response.getWriter().  Consider this JSP:
> 
>   <html>
>   <body>
>   Me first.
>   <% response.getWriter().println("Me second"); %>
>   </body>
>   </html>
> 
>   I was surprised to discover that the out of this is:
> 
>   Me second.
>   <html>
>   <body>
>   Me first.
>   </body>
>   </html>
> 
>   Looking at org.apache.jasper.runtime.PageContextImpl and JspWriterImpl, it appears
that the "out" variable is set up
>   with its own buffer, which explains why text sent straight to the response writer appears
first (before the JSP writer buffer gets flushed).
> 
>   Am I missing something or is this the way it actually works?  If I have it straight,
is this a bug?  I'm not sure about the
>   JSP spec, but other app servers I've tried seem to accumulate text in a single buffer
for both "out" and response.getWriter().
> 
>   Thanks,
> 
>   Karl Goldstein


---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-dev-help@jakarta.apache.org


Mime
View raw message