The filter does get called on my GET request.
In my understanding, a filter is the reccomended method of handling UTF-8 for
both GET & POST (as well as other methods like HEAD, PUT etc.).
When the request is "test.jsp?Name=larevolu%C8%9Bie" as in the example page
the EncodingFilter.doFilter function is being called and it calls
the EncodingFilter.check function as below
Name=request.getParameter("Name");
if (Name!=null)
check(Name, lang);
and the String Name is incorrect as mentionned in the comment below:
void check(String Name, java.util.Locale lang)
{
int c8=Name.codePointAt(8);//c8=200=C8 // should be 539=21Bh = ț
int c9=Name.codePointAt(9);//c9=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);
}
server.xml contains the following:
<Connector port="8060" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" URIEncoding="UTF-8" />
This is because my tomcat 6 is set to work on 8060 with no redirect because
this is a debug setting.
(some time ago I also had tomcat 5 on 8050 but at that time I didn't check the
UTF-8 issue)
Thanks for you help.
Mircea.
________________________________
From: Ake Tangkannaond <[email protected]>
To: Tomcat Users List <[email protected]>
Sent: Wed, 21 April, 2010 12:42:36
Subject: RE: UTF-8 encoding in Tomcat 6.0
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:[email protected]]
Sent: Wednesday, April 21, 2010 3:40 PM
To: [email protected]
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>