You are trying to set the content type on the "real" response after it's
been flushed (if the response is bigger than the output buffer), which
won't do any good because the HTTP headers are long gone.

Try changing the setContentType() method in your wrapper to something like
this:

  public void setContentType(String contentType) {
    super.setContentType(contentType + ";charset=windows-1251");
  }

to set the encoding on the "real" response as soon as the servlet or JSP
page tries to set it on what it thinks the response is (i.e. the wraper
that you've created).

In a production-quality implementation, you would want to add niceties
like:

* Checking for a null argument

* Checking for cases where the character set was already included
  (i.e. a JSP page had "<%@ page contentType="text/html;charset=xxx" %>)

* Overriding setHeader() and addHeader() as well, in case the app
  tries something like:

    response.setHeader("Content-Type", "text/html");

Craig


On Tue, 23 Jul 2002, Dmitry Melekhov wrote:

> Date: Tue, 23 Jul 2002 09:13:56 +0500
> From: Dmitry Melekhov <[EMAIL PROTECTED]>
> Reply-To: Tomcat Users List <[EMAIL PROTECTED]>
> To: tomcat-user <[EMAIL PROTECTED]>
> Subject: RE: filter to set content type
>
> > you need a wrapper for the response object.
> >
>
> Sorry, I can't understand how it works yet :-(
>
> Anyway, I wrote  wrapper:
>
> import javax.servlet.http.*;
>
> public class MyResponseWrapper extends HttpServletResponseWrapper
> {
>
>      public MyResponseWrapper(HttpServletResponse response) {
>        super(response);
>      }
>
>      private String saveContentType = null;
>
>      public String getContentType() {
>        return (this.saveContentType);
>      }
>
>      public void setContentType(String contentType) {
>        super.setContentType(contentType);
>        this.saveContentType = contentType;
>      }
>
> }
>
>
> Then I try to use it:
>
>     public void doFilter(ServletRequest request, ServletResponse response,
>                           FilterChain chain)
>          throws IOException, ServletException {
>        MyResponseWrapper wresponse =
>         new MyResponseWrapper((HttpServletResponse) response);
>
>          chain.doFilter(request, wresponse);
>          wresponse.setContentType("text/xml;charset=windows-1251");
>      }
>
>
> This works for html pages, but doesn't work for jsp!
> I don't know why, but jsps still have type text/html :-(
>
> Could you help me?
>
>
>
>
>
> > see this post in the archives:
> > http://www.mail-archive.com/tomcat-user@jakarta.apache.org/msg41615.html
> >
> > Charlie
> >
> >> -----Original Message-----
> >> From: Dmitry Melekhov [mailto:[EMAIL PROTECTED]]
> >> Sent: Friday, July 19, 2002 1:48 AM
> >> To: [EMAIL PROTECTED]
> >> Subject: filter to set content type
> >>
> >>
> >> Hello!
> >>
> >> I need to filter jsp output to set  encoding in content type.
> >> I tryied several variants I can write ;-) with no success :-(
> >>
> >> Could somebody send me an example?
> >>
> >> Thank you!
> >>
> >>
>
>
>
> --
> To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
> For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>
>
>


--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to