tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Mariusz Wiktorczyk <kos...@fanthom.math.put.poznan.pl>
Subject Re: Character encoding for form submissions
Date Fri, 04 Apr 2003 07:13:36 GMT
Olny filter can help you.
But remember:
------------ from api ---------


      setCharacterEncoding

public void setCharacterEncoding(java.lang.String env)
                          throws java.io.UnsupportedEncodingException

    Overrides the name of the character encoding used in the body of
    this request. This method must be called prior to reading request
parameters or reading input using getReader().

---------------------------

Here is code:
public void doFilter(ServletRequest request, ServletResponse response,
                         FilterChain chain)
    throws IOException, ServletException {

        // Conditionally select and set the character encoding to be used
        if (request.getCharacterEncoding() == null) {
            if (encoding != null)
                request.setCharacterEncoding(encoding);
        }
        // Pass control on to the next filter
        chain.doFilter(request, response);

    }

public void init(FilterConfig filterConfig) throws ServletException {

    this.filterConfig = filterConfig;
        this.encoding = filterConfig.getInitParameter("encoding");
    }

public void destroy() {

        this.encoding = null;
        this.filterConfig = null;

    }

and web.xml
  <filter>
    <filter-name>Set Character Encoding</filter-name>
    <filter-class>filters.SetCharacterEncodingFilter</filter-class>
    <init-param>
      <param-name>encoding</param-name>
      <param-value>UTF-8</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>Set Character Encoding</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

-------------
I had the same problems and this filter has helped me.

Morris Jones wrote:

>I've been researching this for a while.
>
>I'm using Tomcat 4.1.18, and struts 1.0.2.
>
>My app is successfully handling Unicode characters -- in all the actions,
>stored in the database, displayed on the HTML pages, using charset=UTF-8.
>
>But the only way I can find to get form data into Unicode in my form bean
>is by manually transcoding the UTF-8 strings that come in into Unicode.
>
>I'm led to believe that adding a filter to my app that calls
>ServletRequest.setCharacterEncoding("UTF-8") will automatically transcode
>the form data (from POST) into Unicode.  I can set breakpoints in
>the filter and see that setCharacterEncoding() is being called, and
>breakpoints in my form to see that the UTF-8 string from the form is
>being set untouched.
>
>What's the deal?  Is it really necessary to add conversion code to all of
>my form beans?
>
>Cheers,
>Mojo
>  
>




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


Mime
View raw message