Hi Mircea,

 

That filter you wrote is for POST and it has nothing to do with GET request.

 

Have you check the documentation on the conf/server.xml on the following line?

    <Connector port="8080" protocol="HTTP/1.1" 

               connectionTimeout="20000" 

               redirectPort="8443" URIEncoding="UTF-8"/>

 

 

-Ake

 

From: Mircea LUTIC [mailto:mircea_lu...@yahoo.com] 
Sent: Wednesday, April 21, 2010 3:40 PM
To: users@tomcat.apache.org
Subject: UTF-8 encoding in Tomcat 6.0

 

Hello,


I'm having trouble convincing tomcat 6.0 to take UTF-8 strings.
I created an encoding filter with no luck. 

/**
    Here is the Utf8 Filter that fails for me in 
Tomcat 6.0.20 / Eclipse-Galileo / Windows Vista (6.0.6002) <---> Firefox 3.6.3.
        java version "1.6.0_20"
        Java(TM) SE Runtime Environment (build 1.6.0_20-b02)
        Java HotSpot(TM) Client VM (build 16.3-b01, mixed mode)
    Everything is on my  laptop (http://localhost:8060).
    
    What I do is create a GET string (based on parameters from a 
showModalDialog)
    by script and send it (from Firefox) to the (localhost) server 
    which is running under eclipse with a breakpoint in the EncodingFilter.
    
    Note that the system locale is ro_RO (may have an influence)

 * 
 * set in appl/WebContent/web.xml:
 * 
 <filter>
    <filter-name>EncodingFilter</filter-name>
    <filter-class>ro.lutic.transcode.EncodingFilter</filter-class>
    <init-param>
        <param-name>encoding</param-name>
        <param-value>UTF-8</param-value>
    </init-param>
</filter>

 * 
 * set in tomcat/conf/server.xml at <Connector protocol="HTTP/1.1
 * 
    URIEncoding="UTF-8"   

 */


public class EncodingFilter implements Filter 
{
    private String encoding;
    private FilterConfig filterConfig;
    public EncodingFilter() {}
    public void destroy() {}

    public void init(FilterConfig fc) throws ServletException 
    {
        this.filterConfig = fc;
        this.encoding = filterConfig.getInitParameter("encoding");
    }
    
    public void doFilter
        (ServletRequest  request
        ,ServletResponse response
        , FilterChain chain
        ) throws IOException, ServletException 
    {
        String clientEncoding=request.getCharacterEncoding();
        String Name;

        String fileEncoding=System.getProperty("file.encoding");
        if (fileEncoding==null || !fileEncoding.equals("UTF-8"))// eclipse says 
it's already UTF-8
            System.setProperty("file.encoding", "UTF-8");    // so this never 
gets executed
        
        if(null == clientEncoding)    // this is always null
             request.setCharacterEncoding(encoding);// so this is always set
             
        //String uri=request instanceof 
HttpServletRequest?((HttpServletRequest)request).getRequestURI():null;if 
(uri!=null)uri+="";
        //window.location=scriptCreatedUrl;
        // using alert(scriptCreatedUrl) I can see that
        // GET sent Name=revoluție as Name=revolu%C8%9Bie (correct) 
        
        Name=request.getParameter("Name");
        if (Name!=null)
        {
            java.util.Locale lang=request.getLocale();//ro_RO
            check(Name, lang);
                
            check("larevoluție"   , lang);// these are ok
            check("_el_piraña"    , lang);
            check("_qi_gong气功"  , lang);
            check("_psyche_ψυχή"  , lang);
            check("_shalom_שָׁלוֹם"  , lang);
            check("_salaam_سَلاَمٌ"   , lang);
            check("_Baikal_Байка́л", lang);
        }

        chain.doFilter(request, response);
    }
    void check(String Name, java.util.Locale lang)
    {
        int c8=Name.codePointAt(8);//c6=200=C8 // should be 539=21Bh = ț
        int c9=Name.codePointAt(9);//c7=155=9B // should be 105= 69h = i
        char ch[]={Name.charAt(8),Name.charAt(9)};
        int x=c8+c9;    //to have eclipse (Galileo) display the above
        String s=new String(ch); 
    }
}
/*
 *     also typed "about:config" in the (Firefox) address bar.
    Used the filter input to search for 
"network.standard-url.encode-query-utf8" property.
    turned it to true & restarted the browser: no luck
 */


Here is also a page that sends a failing request:

<%@ page language="java"
    pageEncoding="UTF-8" 
    contentType="text/html; charset=UTF-8"
 %>

http://www.w3.org/TR/html4/loose.dtd";>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>UTF-8</title>
<script>
function send()
{
    
window.location="test.jsp?Name=larevolu%C8%9Bie";//"test.jsp?Name="+escape(Utf8encode("larevoluție"))
}
</script>
</head>
<body>
<button onclick="send()">send</button>
</body>
</html>

Reply via email to