2

I'm in desperate need of help.

I have a Java servlet that is accessed by a HTTP Get URL with eight parameters in it. The problem is that the parameters are not exclusive to English. Any other language can be in those parameters, like Hebrew, for example.

Now, when I send the data - either from the class that is supposed to send the Get request or simply from the browser's address bar, the query that I see in debug (Netbeans) has gibberish characters and the Parameters themselves, post request.getParameter are ???? question marks.

How or where would I go about solving this?

Cypher_CS
  • 55
  • 1
  • 8
  • 1
    BTW, don't use POST for something that should, semantically, be a GET request. Among other problems, it breaks bookmarks and the back button. – erickson Sep 14 '09 at 15:25

4 Answers4

0

There should be a setting in your servlet engine that controls the character encoding to use for URL parameters. To support every language, the UTF-8 encoding is a great choice—but this isn't the default encoding for many servers.

Tomcat uses ISO-8859-1 a the default character decoder for URL parameters, regardless of the encoding of the page that contains the URL. This can be changed with the "URIEncoding" attribute in its Connector configuration. Other application servers may have similar settings.

If the server is expecting UTF-8 encoded parameters, the page that is sending them should be encoded with UTF-8 too.

This article covers many problems commonly encountered when working with JSP.

erickson
  • 265,237
  • 58
  • 395
  • 493
0

I suspect your server is in UTF-8 mode but your URL is encoded as Latin-1. What kind of question marks are you getting, "?" or "�" ? This will shed light on who is doing the conversion.

You need to change your connector to Latin-1 by adding URIEncoding="ISO-8859-1" attribute, or you can re-encode the URL in UTF-8.

If the URL is encoded by browser, you are hosed. There is no solution because it could go either way depending on the browsers. See my question,

Handling Character Encoding in URI on Tomcat

Community
  • 1
  • 1
ZZ Coder
  • 74,484
  • 29
  • 137
  • 169
0

In your servlet, before request.getParameter("parameter-name"), set the character encoding of your request to "UTF-8", like so:

request.setCharacterEncoding("UTF-8");
String word = request.getParameter("parameter-name");

See this Tomcat FAQ about Character Encoding issues for details.

Shant Dashjian
  • 888
  • 11
  • 20
0

You shall add connector settings in tomcat's server.xml, as this is problem at tomcat level.

Please add following in connector tag

URIEncoding="UTF-8" useBodyEncodingForURI="true"

if the requests are submitted from the browser then you might have to do some changes in web.xml also. Add this filter tag in web.xml

<filter> <filter-name>encodingFilter</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> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter>
 <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>

Hope this helps.

singh
  • 1,020
  • 7
  • 8