Write a filter that converts all values to utf-8

package com.sgccir.init;


import java.io.*;
import javax.servlet.*;


/**
 * Filter that unconditionally sets the character encoding to be used
 * in parsing the incoming request to a value specified by the
 * <strong>encoding</strong> filter initialization parameter in the web app
 * deployment descriptor (</code>/WEB-INF/web.xml</code>).  This filter
could
 * easily be extended to be more intelligent about what character encoding
to
 * set, based on characteristics of the incoming request (such as the values
 * of the <code>Accept-Language</code> and <code>User-Agent</code> headers,
 * or a value stashed in the current user's session).
 *
 */

public class SetCharacterEncodingFilter implements Filter {


    /**
     * The default character encoding to set for requests that pass through
     * this filter.
     */
    protected String encoding = null;


    /**
     * The filter configuration object we are associated with.  If this
value
     * is null, this filter instance is not currently configured.
     */
    protected FilterConfig filterConfig = null;



    /**
     * Take this filter out of service.
     */
    public void destroy() {

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

    }


    /**
     * Select and set (if specified) the character encoding to be used to
     * interpret request parameters for this request.
     *
     * @param request The servlet request we are processing
     * @param result The servlet response we are creating
     * @param chain The filter chain we are processing
     *
     * @exception IOException if an input/output error occurs
     * @exception ServletException if a servlet error occurs
     */
    public void doFilter(ServletRequest request, ServletResponse response,
                         FilterChain chain)
        throws IOException, ServletException {

        // Select and set (if needed) the character encoding to be used
        String encoding = selectEncoding(request);
        if (encoding != null)
            request.setCharacterEncoding(encoding);

        // Pass control on to the next filter
        chain.doFilter(request, response);

    }


    /**
     * Place this filter into service.
     *
     * @param filterConfig The filter configuration object
     */
    public void init(FilterConfig filterConfig) throws ServletException {

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

    }


    // ------------------------------------------------------ Protected
Methods


    /**
     * Select an appropriate character encoding to be used, based on the
     * characteristics of the current request and/or filter initialization
     * parameters.  If no character encoding should be set, return
     * <code>null</code>.
     * <p>
     * The default implementation unconditionally returns the value
configured
     * by the <strong>encoding</strong> initialization parameter for this
     * filter.
     *
     * @param request The servlet request we are processing
     */
    protected String selectEncoding(ServletRequest request) {

        return (this.encoding);

    }


}

and add this to your web.xml 

  <filter-mapping>
    <filter-name>Set Character Encoding</filter-name>
    <servlet-name>action</servlet-name>
  </filter-mapping>

the action is the name of struts controller.

-----Original Message-----
From: Adam Hardy [mailto:[EMAIL PROTECTED] 
Sent: Monday, March 01, 2004 12:17 PM
To: Struts Users Mailing List
Subject: Re: Problems with UTF-8 and forms

On 03/01/2004 12:29 AM Jon Bohm wrote:
> BUT if I rewrite my custombean's getValue method it all works fine (except
> for swedish uppercase letters):
> 
> public String getValue()
> {
>     return new String( value.getBytes(), "UTF-8");
> }

Still shouldn't be necessary.

> Apache Tomcat/5.0.12
> Java 1.4.2_01-b06
> Struts 1.1 (I think - how do I check?)
> Windows XP (I know, I'm a Linuxdude gone bad)

Open up struts.jar in winzip or something and view META-INF/MANIFEST.mf 
- check implementation-version.

So what does the debugging say at the end of your JSPs? Is your response 
UTF-8 or iso-xxxx encoded? I bet it says the page content-type is still 
iso-8859.

How are you setting your struts controller parameter (in struts-config.xml):

<set-property property="contentType"
   value="text/html; charset=UTF-8"/>

Do you have any locale-encoding-mapping-list in your web.xml? You don't 
need it with UTF-8. You also don't need to specify it in any 
jsp-property-group since the struts controller will handle it.


>>>http://www.anassina.com/struts/i18n/i18n.html
>>
>>Good link, but getting a little out-of-date now.
> 
> 
> You have a better one? :)

I wish! If you get this solved, we can condense this thread and post it 
in the struts wikki.

Adam
-- 
struts 1.1 + tomcat 5.0.16 + java 1.4.2
Linux 2.4.20 Debian


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

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

Reply via email to