0

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

 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.

2
  • Can you expand at the beginning of servlet but didn't help. Commented Jun 28, 2011 at 12:59
  • added that line to servlet code. Commented Jun 28, 2011 at 13:04

2 Answers 2

2

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.
Sign up to request clarification or add additional context in comments.

Comments

1

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>

2 Comments

This does basically only req.setCharacterEncoding("UTF-8");. However you said by yourself that it didn't work. Perhaps you were not running the code you think you're running, or it is actually not at the beginning of the servlet.
ok. This change fixed the whole issue of character conversion. Before I was doing it manually for each getParameter.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.