You are viewing a plain text version of this content. The canonical link for it is here.
Posted to general@jakarta.apache.org by ar...@suvi.kas.utu.fi on 2000/10/12 10:09:26 UTC
Header&footer filter (tomcat 4.0-m2)
Why won't this filter work? Requested page is /test.jsp
-----------------------------------
public void doFilter(ServletRequest request, ServletResponse response)
throws IOException, ServletException {
RequestDispatcher
disp=request.getRequestDispatcher("/header.html");
disp.include(request, response);
Filter next = filterConfig.getNext();
next.doFilter(request, response);
disp=request.getRequestDispatcher("/footer.html");
disp.include(request, response);
}
-----------------------------------
It throws an exception:
java.lang.IllegalStateException: getOutputStream() has already been called
for this response
at
org.apache.catalina.connector.ResponseBase.getWriter(ResponseBase.java:636)
at
javax.servlet.ServletResponseWrapper.getWriter(ServletResponseWrapper.java:133)
at beans.RespWrapper.getWriter(RespWrapper.java:27)
at
org.apache.jasper.runtime.JspWriterImpl.initOut(JspWriterImpl.java:166)
at
org.apache.jasper.runtime.JspWriterImpl.flushBuffer(JspWriterImpl.java:158)
at
org.apache.jasper.runtime.JspWriterImpl.flush(JspWriterImpl.java:205)
...
Re: Header&footer filter (tomcat 4.0-m2)
Posted by Arvi Leino <ar...@suvi.kas.utu.fi>.
On Thu, 12 Oct 2000, Craig R. McClanahan wrote:
> It is not allowed to call request.getRequestDispatcher() from within a
> Filter,
> because you are not running inside a servlet at that moment (I will make
> sure this
> clarification gets added to the spec).
>
> - -
>
> When I get a chance to write one, I will include an example of this sort
> of thing
> in Tomcat.
Thanks! What about setting request attribute for original request, calling
template jsp and template calling original request?
Arvi
Re: Header&footer filter (tomcat 4.0-m2)
Posted by "Craig R. McClanahan" <Cr...@eng.sun.com>.
arvleino@suvi.kas.utu.fi wrote:
> Why won't this filter work? Requested page is /test.jsp
>
> -----------------------------------
> public void doFilter(ServletRequest request, ServletResponse response)
> throws IOException, ServletException {
> RequestDispatcher
> disp=request.getRequestDispatcher("/header.html");
> disp.include(request, response);
>
> Filter next = filterConfig.getNext();
> next.doFilter(request, response);
>
> disp=request.getRequestDispatcher("/footer.html");
> disp.include(request, response);
> }
> -----------------------------------
>
> It throws an exception:
>
> java.lang.IllegalStateException: getOutputStream() has already been called
> for this response
> at
> org.apache.catalina.connector.ResponseBase.getWriter(ResponseBase.java:636)
> at
> javax.servlet.ServletResponseWrapper.getWriter(ServletResponseWrapper.java:133)
> at beans.RespWrapper.getWriter(RespWrapper.java:27)
> at
> org.apache.jasper.runtime.JspWriterImpl.initOut(JspWriterImpl.java:166)
> at
> org.apache.jasper.runtime.JspWriterImpl.flushBuffer(JspWriterImpl.java:158)
> at
> org.apache.jasper.runtime.JspWriterImpl.flush(JspWriterImpl.java:205)
> ...
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: general-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: general-help@jakarta.apache.org
It is not allowed to call request.getRequestDispatcher() from within a
Filter,
because you are not running inside a servlet at that moment (I will make
sure this
clarification gets added to the spec).
If you want to use a filter to add header/footer text, you will need to
use the
following strategy in your doFilter() method:
* Read the text of your header into an internal buffer (or at
least count the characters so you know what the ultimate
content length will be.
* Create a wrapper around the response object you received
(extends javax.servlet.http.HttpServletResponseWrapper)
that will be passed on to the rest of the filter chain.
* This wrapper should just buffer up the output created by the
servlet itself.
* After the servlet returns, figure out how many characters are
in the footer text as well.
* On the real response object:
- Set the content length to the sum of the three sizes
(header text, text returned by the servlet, and footer text)
- Write all the text (header, servlet, footer) in order
When I get a chance to write one, I will include an example of this sort
of thing
in Tomcat.
Craig McClanahan
====================
See you at ApacheCon Europe <http://www.apachecon.com>!
Session VS01 (23-Oct 13h00-17h00): Sun Technical Briefing
Session T06 (24-Oct 14h00-15h00): Migrating Apache JServ
Applications to Tomcat