3

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.

sass
  • 309
  • 2
  • 8
  • 20

3 Answers3

4

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:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Thanks a lot, for the answer. I have edited my original post - could you have a look at it? Thanks in advance. – sass Oct 10 '10 at 11:55
  • The webbrowser has aborted the request by either pressing `Esc`, navigating away or being closed. – BalusC Oct 10 '10 at 13:59
  • Thanks. The encoding is still wrong but I also noticed that it works correctly one or two times directly after the restart of a server. Have you ever observed such behaviour? – sass Oct 10 '10 at 14:21
  • Do you see any difference in request/response headers? The *Net* panel of [Firebug](http://getfirebug.com) is helpful in this. – BalusC Oct 10 '10 at 15:22
  • I have edited the post again and added full response and request headers. – sass Oct 10 '10 at 15:39
  • Yes, looks like it. I should probably ask a fellow programmer to have a look at the code as I cannot post the whole project here. Thanks a lot for help. – sass Oct 10 '10 at 16:12
1

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>    
Milan Švec
  • 1,675
  • 17
  • 21
1

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.

https://rogerkeays.com/servletrequest-setcharactercoding-ignored

Roger Keays
  • 3,117
  • 1
  • 31
  • 23