1

I've some issues with my Java Servlet if it's called with special chars (like Æ, Ø og Å) in the GET-parameters: http://localhost:8080/WebService/MyService?test=Øst.

I than have this code in my doGet:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    System.out.println(request.getParameterValues("test")[0]);
}

The messages printed in the console is: Ã?st.

The Web Service should be able to handle calls like this. How can I encode the parameter values in a proper way?

dhrm
  • 14,335
  • 34
  • 117
  • 183

3 Answers3

2

This needs to be configured at servet level. It's not clear which one you're using, so I'll give examples for Tomcat and Glassfish only.

Tomcat: add URIEncoding attribute to <Connector> element in /conf/server.xml:

<Connector ... URIEncoding="UTF-8">

Glassfish: add <parameter-encoding> to /WEB-INF/glassfish-web.xml (or sun-web.xml for older versions):

<parameter-encoding default-charset="UTF-8" />

See also:

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • I'm using a Tomcat server using the Eclipse IDE. The file *WebContent/WEB-INF/web.xml* does not contain any Connector-element. Is it normal? – dhrm Mar 21 '12 at 15:05
  • I said `/conf/server.xml`, not `/WEB-INF/web.xml`. It's in Tomcat installation folder. However, if you haven't taken over Tomcat fully in Eclipse (which is the default setting; see also http://stackoverflow.com/questions/2280064/tomcat-started-in-eclipse-but-unable-to-connect-to-link-to-http-localhost8085/2280798#2280798), then you need to configure it by the `server.xml` in *Servers* project instead. Note that for production Tomcat this needs to be configured in its `/conf/server.xml`. – BalusC Mar 21 '12 at 15:08
1

you should be percent encoding special characters (http://en.wikipedia.org/wiki/Percent-encoding). In your example above, the "slashed O" (Ø) has the UTF-8 code 0xd8, so your URL would properly be written:

http://localhost:8080/WebService/MyService?test=%d8st.

Which should result in

Øst.

being printed to the console, from your servlet code above.

bryon
  • 11
  • 1
  • A service like Google Maps works without percent encoding special chars: http://maps.googleapis.com/maps/api/directions/json?origin=9220+Aalborg+Øst&destination=Copenhagen&sensor=false&region=dk. I would like to support this too. – dhrm Mar 21 '12 at 15:01
-1

You could try the following code before requesting parameters:

request.setCharacterEncoding("utf-8");
Alexey A.
  • 1,389
  • 1
  • 11
  • 20