6 Replies Latest reply: Jul 30, 2011 5:20 AM by 879158 RSS

    JSP charset problem with ISO-8859-7 (Greek) using post via XMLHttpRequest

    843840
      Hello everybody.

      I am developing web applications in Greek. I have an MVC model. A JSP page (jsp1) posts a form using XMLHttpRequest object to a servlet. The javascript code iterates through the elements of the for and makes a string like:

      FIRST_NAME=Arthur&LAST_NAME=Myller...

      but in Greek names and characters. This sting is put to the body of the XMLHttpRequest and then gets posted to a servlet.

      Then the servlet takes it and does some database inserts and updates and then the request from the XMLHttpRequest object, is forwarded as it is to another jsp page (ssp2) to show the results on the screen.
      Finally the html that is produced from jsp2 is returned to the XMLHttpRequest object and put to a div element on jsp1 using div1.innerHTML = [jsp2 html contents].

      The problem is that The Greek characters on jsp2 that came from jsp1 are shown like ??????.

      both jsp1 and jsp2 pages have a directive
      <%@ page language="java" contentType="text/html pageEncoding="ISO-8859-7"%>

      Withput pageEncoding="ISO-8859-7" the hardcoded Greek labels cannot be shown.

      I have tried request.setCharacterEncoding("ISO-8859-7"); on all three (jsp1, servlet and jsp2) but it doesn't work.

      Any ideas? I am really stuck here.


      Thanx in advance
        • 1. Re: JSP charset problem with ISO-8859-7 (Greek) using post via XMLHttpRequest
          mrityunjoy
          Couple of things :
          a. You must consider setting request encoding, page encoding and response encoding.
          b. Whether database setup is done to store and retrieve Greek characters. You can test it separately. And it is surely not the culprit for not displaying data in JSP page received from another JSP.
          c. Server setup is done to support ISO-8859-7 character set. For example, in tomcat conf/server.xml the <connector > should have attribute URIEncoding set to appropriate character set. For example:
          <Connector port="8080"
                         maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
                         enableLookups="false" redirectPort="8443" acceptCount="100"
                         debug="0" connectionTimeout="20000" 
                         disableUploadTimeout="true" 
                         URIEncoding="UTF-8"/>
          d. In case application code manipulates (byte to string or string to byte conversion) Greek string data, it must consider appropriate character set whenever necessary.

          I suggest one more thing: Without XMLHttpRequest test your JSP pages normally to verify your application works with Greek data.

          Thanks,
          Mrityunjoy
          • 2. Re: JSP charset problem with ISO-8859-7 (Greek) using post via XMLHttpRequest
            843840
            Thank you for your answer.

            Well the number (a) of your answer i have tested strongly.
            In (b) i agree it is irelevant, i just mentioned it
            (d) is perhaps out of the question. It is a very simple process of transferring data. Whatever string comes, goes to the jsp2.

            I have already tested it without XMLHttpRequest by <input type="submit"... and it works fine. That is why i mentioned all the proccess that i used on XMLHttprequest.

            But another thing has come to surface. I tested it with firefox and it works fine bot with XMLHttpRequest and submit button. Only Internet explorer has this problem with XMLHttpRequest. I think i must search the cause on the character encoding manipulation of explorer and how it reacts with div.innerHTML.

            If there are any suggestions, i am all ears....
            • 3. Re: JSP charset problem with ISO-8859-7 (Greek) using post via XMLHttpRequest
              mrityunjoy
              Try setting request header in XMLHttpRequest in Javascript:
              var client = new XMLHttpRequest();
              client.open("POST", "/log");
              client.setRequestHeader("Content-Type", "text/plain;charset=UTF-8");
              client.send(message);
              Thanks,
              Mrityunjoy
              • 4. Re: JSP charset problem with ISO-8859-7 (Greek) using post via XMLHttpRequest
                843840
                It worked!

                Actually since there i a body with PARAM_NAME=PARAM_VALUE on the XMLHttpRequest there must be

                this.xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");

                If i write
                this.xmlHttp.setRequestHeader("Content-Type", "text/html; charset=UTF-8");
                the params will not be transfered and be null when i receive them in java code.

                I have tried exactly the same with ISO-8879-7 but not with UTF-8. It works fine with this simple example that i have discussed before as well
                as with more complex MVC that i have already developed and just waiting for the correction.
                I have tried it with Internet Explorer as well as Firefox.

                Thank you for your answer. You really helped me with this mess. When i get the time i must study a little about character encodings....
                • 5. Re: JSP charset problem with ISO-8859-7 (Greek) using post via XMLHttpRequest
                  mrityunjoy
                  Thanassis.Diamantis wrote:
                  It worked!
                  Good.
                  Thank you for your answer. You really helped me with this mess. When i get the time i must study a little about character encodings....
                  You are welcome.

                  Thanks,
                  Mrityunjoy
                  • 6. Re: JSP charset problem with ISO-8859-7 (Greek) using post via XMLHttpRequest
                    879158
                    Hi

                    Could you give some more details about how you fixed that?

                    I have the same problem when i am trying to print greek in a jsp through a servlet and its driving me crazy...

                    Thanks in advance