问题
I'm writing an application in JSF 2.0 which supports many languages, among them ones with special characters. I use String value = request.getParameter("name") and POST method, the page encoding is set to UTF-8 and the app is deployed on apache tomcat 6 which has the connector set correctly to utf-8 in a server.xml file:
<Connector URIEncoding="utf-8" connectionTimeout="20000" port="8088" protocol="HTTP/1.1" redirectPort="8443"/>
Yes I get strange results like ä for example in place of expected special characters. What should I check next to get rid of the problem? Thanks for help and suggestions.
--------------Edit-------------------
This is the response header:
Server Apache-Coyote/1.1
Content-Type text/html;charset=UTF-8
Content-Length 5160
Date Sun, 10 Oct 2010 15:32:24 GMT
And the request header:
Host localhost:8088
User-Agent Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.10) Gecko/20100914 Firefox/3.6.10 ( .NET CLR 3.5.30729; .NET4.0E)
Accept text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language pl,en-us;q=0.7,en;q=0.3
Accept-Encoding gzip,deflate
Accept-Charset ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive 115
Connection keep-alive
Referer http://localhost:8088/Panel/addbuilding.jsp
Cookie JSESSIONID=DD85C95BDFEA9E51B5E4146F2643295D
Pragma no-cache
Cache-Control no-cache
Best Regards, sass.
回答1:
The URIEncoding="utf-8"
has only effect on GET requests. GET comes with the parameters in the request URL (as you see in browser address bar). POST request parameters are however in the request body, not in the request URL. You want to use request.setCharacterEncoding("UTF-8") here. You can do it in a Filter
class which is mapped on the FacesServlet
.
However, by default JSF should already have taken care about this based on the page encoding (if you're using Facelets). Are you sure that it isn't just the stdout/logging console which is using the wrong encoding to display the parameters?
See also:
- Unicode - How to get characters right?
回答2:
I spend lot of time finding solution for this issue. It seems that there is a bug somewhere in JSF2/Tomcat/Glassfish. Parameter values are corrupted only in the first call after jsf session is created. The subsequent calls work fine - request headers are same as in the first (not working) request, except existence of jsession id. It also proves that it is NOT a problem of wrong configuration for utf-8.
I had to implement different solutions for Glassfish and Tomcat. After these changes everything works fine. Of course, this is workaround, the only correct solution is to fix the bug.
Glassfish: additional line in glassfish-web.xml
<parameter-encoding default-charset="UTF-8" />
Tomcat: I did not find equivalent config param for Tomcat, I had to add additional servlet filter
package cz.cksvec.filters;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class UTF8Filter implements Filter {
@Override
public void init(FilterConfig fc) throws ServletException {}
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
chain.doFilter(request, response);
}
@Override
public void destroy() {}
}
and web.xml section to activate the filter:
<filter>
<filter-name>UTF8Filter</filter-name>
<filter-class>cz.cksvec.filters.UTF8Filter</filter-class>
</filter>
<filter-mapping>
<filter-name>UTF8Filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
回答3:
As BalusC says, the tomcat configuration is only for GET requests. For POSTing multipart/form-data you need to make a filter which calls
request.setCharacterEncoding("UTF-8");
before any call to request.getParameter() is made.
You can cut and paste from my blog if you want to try it out quickly.
http://www.ninthavenue.com.au/servletrequest-setcharactercoding-ignored
来源:https://stackoverflow.com/questions/3897301/jsf-2-0-request-getparameter-return-a-string-with-wrong-encoding