Announcement Announcement Module
Collapse
No announcement yet.
UTF-8 data in a GET request Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • UTF-8 data in a GET request

    I have a page in my project that wants to be able to accept arbitrary UTF-8 text as a GET parameter. Something like this:

    http://mysite.com/?name=%e6%99%82

    Those three characters together comprise the Japanese character "時" in the UTF-8 encoding.

    What I find is that in my controller method, those bytes have apparently been forced into an ISO-8859-1 interpretation. I need to do this to recover the correct string:

    name = new String(name.getBytes("ISO-8859-1"), "UTF-8");

    I recently added a CharacterEncodingFilter to my project's main web.xml file, but that seems to only take care of POSTs. Is there any way that I can arrange for GETs to be decoded as UTF-8 as well?

  • #2
    I have the exact same problem. Have you found a solution yet ?

    Thanks,
    Romain.

    Comment


    • #3
      No, I couldn't find any better approach, nor was I informed of one.

      Actually, it has recently come to my attention that the behavior of the String constructors that take an encoding name is undefined in the presence of invalid data, so it'd be better to use the facilities in java.nio.charset to do the conversion.

      Comment


      • #4
        Thanks for the reply.

        Using custom decoding is unfortunately not the easy path on my setup because the parameter is retrieved via the @RequestParam annotation...

        Comment


        • #5
          For Websphere if neither charset nor the default.client.encoding system property is set, the application server uses the ISO-8859-1 character set.

          -Dclient.encoding.override=UTF-8 generic JVM argument solves the problem.

          Comment


          • #6
            Thanks for the info.

            I had finally solved the issue by setting the URIEncoding="UTF-8" attribute in the HTTP Connector of my TomCat configuration.
            See http://wiki.apache.org/tomcat/FAQ/CharacterEncoding

            All my JSP are also defined with a UTF8 page encoding (via the jsp-config element of my web.xml).

            I had tried this before, but it didn't work at first because I had not cleaned the server configuration cached in my IDE (Eclipse + WTP). After I cleaned the server it worked well with the config above.

            Romain.

            Comment


            • #7
              I run into the same problem before. It is not Spring related. The following shall solve the problem:
              Code:
              new String((ServletRequestUtils.getStringParameter(request, "tag")).getBytes("ISO-8859-1"), "UTF-8");
              and try this one: http://homepage.kgbinternet.com/dire...9F%B3%E4%B9%90

              HTH

              Comment


              • #8
                Hi,

                tomcat default query string encoding is set to ISO-8859-1. To change this to UTF-8 add URIEncoding attribute to connector tag in server.xml config. Now you can use getParamter method as usual.

                Comment


                • #9
                  Thanks all for the answers.

                  I had resolved the issue with the URIEncoding=UTF-8 setting in TomCat, and also declared the encoding of my JSPs to UTF-8 with the web.xml jsp-config element.

                  It didn't work at first because the tomcat configuration was cached in my IDE (Eclipse+WTP), but it worked after I cleaned the server.

                  Romain.

                  Comment


                  • #10
                    Hi guys:

                    I had this problem as well but my application I am writing has to work regardless of application server settings.

                    Is there a way to determine the encoding for each parameters?

                    I have tried to used request.getCharacterEncoding() but I got UTF-8, which is incorrect because the parameter is actually encoded in ISO-8859-1.

                    I have also tried using request.getHeaders("Content-Type") but I actually got null.

                    Comment

                    Working...
                    X