开发者

req.getParameter returns values wrong character encoding

开发者 https://www.devze.com 2023-03-16 09:00 出处:网络
I\'m trying to get values from a JSP using getParameter which includes ü,é,à etc. But get wrong values in servlet.I\'ve checked the content type with firebug and found that

I'm trying to get values from a JSP using getParameter which includes ü,é,à etc. But get wrong values in servlet. I've checked the content type with firebug and found that

Content-Type    text/html;charset=UTF-8

checked the POST section with firebug and found the correct value there, when I try to access it in servlet it is wrong. Gives ö instead of ö

req.getCharacterEncoding(); 

returns null.

Tried with setting 开发者_JAVA技巧

 req.setCharacterEncoding("UTF-8");

at the beginning of servlet but didn't help.

edited:

req.getParameter("myValue").getBytes("8859_1"), "utf-8")

above line gives correct value.


From http://wiki.apache.org/tomcat/FAQ/CharacterEncoding

The character set for HTTP query strings (that's the technical term for 'GET parameters') can be found in sections 2 and 2.1 the "URI Syntax" specification. The character set is defined to be US-ASCII. Any character that does not map to US-ASCII must be encoded in some way. Section 2.1 of the URI Syntax specification says that characters outside of US-ASCII must be encoded using % escape sequences: each character is encoded as a literal % followed by the two hexadecimal codes which indicate its character code. Thus, a (US-ASCII character code 97 = 0x61) is equivalent to %61. There is no default encoding for URIs specified anywhere, which is why there is a lot of confusion when it comes to decoding these values.

The page does mention 2 possible ways to influence this behavior when running on a Tomcat servlet running:

  • Set the URIEncoding attribute on the element in server.xml to something specific (e.g. URIEncoding="UTF-8").
  • Set the useBodyEncodingForURI attribute on the element in server.xml to true. This will cause the Connector to use the request body's encoding for GET parameters.


adding the following to web.xml fixed the issue...

<filter>
    <filter-name>charsetFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
        <param-name>encoding</param-name>
        <param-value>UTF-8</param-value>
    </init-param>
</filter>

<filter-mapping>
    <filter-name>charsetFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号