4 Replies Latest reply on Sep 8, 2005 6:58 PM by Joe Germuska

    Get parameters from request decoded with utf-8

    Magne Skjeret Newbie

      Hi all

      I have applied a filter in front of every request to ensure that the input stream is always read as UTF-8.
      I do this with request.setCharacterEncoding("UTF-8") as the first filter in the chain.

      This works fine in a standalone tomcat I am using (4.04 yup old one).

      When I deploy my war in jboss, the request is decoded as iso-8859-1 regardless to what I set. Jboss is version 325 and tomcat is 5.0.26.

      That leads me to think that jboss(or tomcat) has accessed the request somehow before the filter is reached.

      Does anyone know if my assumptions are correct, and if, how do I make the request be read as utf-8.

      Thanks in advance
      Magne

        • 1. Re: Get parameters from request decoded with utf-8
          Scott Stark Master

          Create a bug report with an example of what your doing so we can look into it:
          http://jira.jboss.com/jira/browse/JBWEB

          • 2. Re: Get parameters from request decoded with utf-8
            Olga Agady Newbie

            try this (for example you want to get the field "message" from the request):

            request.setCharacterEncoding("utf-8");
            String message = new String(request.getParameter("message").getBytes("ISO-8859-1"),"utf-8");

            It works for me. Just setting the character encoding didn't work. I ran this code in jboss 3.0, it worked. Now I run it in 4.0 - works the same way.

            Olga

            • 3. Re: Get parameters from request decoded with utf-8
              Magne Skjeret Newbie

              I dont think that code will work in any situation, I might be wrong though.
              It looks a bit dangerous at least, and a bit hard to apply everywhere.

              But I have found a bulletproof solution for this (the only solution I think).

              I used to have this working in tomcat 4.
              I had a encoding filter applied in front of the servlet, setting the
              request.setCharacterEncoding("UTF-8") before anything had accessed the request input stream.

              This just stopped working suddenly when upgrading to tomcat 5.0.26.
              It seems that they had removed the request.setCharacterEncoding support, and had applied something to the Connector element in server.xml of tomcat.

              My Connector now looks like this in conf/server.xml

              <Connector port="80" maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
               enableLookups="false" redirectPort="8443" acceptCount="100"
               debug="0" connectionTimeout="20000" URIEncoding="UTF-8"
               disableUploadTimeout="true" />


              Look at URIEncoding="UTF-8"..

              request.getParameter("word") will now give me the UTF-8 decoded string.
              You need then to make sure that each url in <a href is properly encoded.
              I think form submit will be correctly encoded in utf-8 if the meta tag says the page is in utf-8, at least that is what I see in firefox and IE.

              Thanks
              Magne



              • 4. Re: Get parameters from request decoded with utf-8
                Joe Germuska Newbie

                 

                "mskjeret" wrote:

                My Connector now looks like this in conf/server.xml
                <Connector port="80" maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
                 enableLookups="false" redirectPort="8443" acceptCount="100"
                 debug="0" connectionTimeout="20000" URIEncoding="UTF-8"
                 disableUploadTimeout="true" />


                Look at URIEncoding="UTF-8"..


                A solution which sets the URIEncoding value for the Tomcat connector will only apply to GET parameters. (I don't know if the "Get" in the original message subject was referring to the HTTP request method, but I assumed it was more like "retrieve") POST parameters are sent with the encoding explicitly marked as part of the content-type, conventionally according to the encoding of the page in which the form was presented.

                Our attempt to implement a ServletFilter calling setRequestEncoding does not seem to be invoked when run under JBoss 3.2.5, although the same exact web application deployed to a standalone Tomcat 5.0.25 server works correctly.

                If anyone has similar experience, please advise.