How to handle special chars in parameter values?

前端 未结 3 713
闹比i
闹比i 2020-12-22 04:00

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=Øs

相关标签:
3条回答
  • 2020-12-22 04:30

    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:

    • Unicode - How to get the characters right? - JSP/Servlet request
    0 讨论(0)
  • 2020-12-22 04:31

    You could try the following code before requesting parameters:

    request.setCharacterEncoding("utf-8");
    
    0 讨论(0)
  • 2020-12-22 04:36

    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.

    0 讨论(0)
提交回复
热议问题