1

I´ve problems when getting a String in the request of an ajax Call. The String have the word, as example, "Ñoño" but when i read the parameter using:

String parameter = request.getParameter("param");

I have strange characters as a result

So i´ve added the next code

byte [] parameterByte= parameter.getBytes();        
parameter = new String(parameterByte,"UTF-8");

As a result i get most of the character as supposed, except the Ñ. Why i´m having problems with this character? Any idea?

Regars.

Keetah
  • 261
  • 1
  • 5
  • 15

2 Answers2

2

I solve the problem doing:

byte [] parameterByte= parameter.getBytes("ISO-8859-15");        
parameter = new String(parameterByte,"UTF-8");

The question is, why i have to do this? what is wrong in my enviroment/code that causes this problem? any idea?

Keetah
  • 261
  • 1
  • 5
  • 15
  • Check http://stackoverflow.com/questions/1006276/what-is-the-default-encoding-of-the-jvm. Each time you don't specify your encoding, you are using default. – fglez May 03 '13 at 12:36
  • Note that this is not a solution. This is a workaround/hack. A rather ugly and unportable one, either. – BalusC May 03 '13 at 14:05
  • @BalusC Absolutely agree! just a ugly workaround. I´ll try to apply your suggestions, but the application is really big, and i´m a little worried that this changes creates new bugs in old bad programmed functions. Regards.- – Keetah May 03 '13 at 17:38
1

There are in this particular case two places where you need to control the encoding:

  1. The HTTP response encoding of the page which initiates the form submits and/or ajax calls needs to be set to UTF-8. The webbrowser will use the very same encoding to encode the outgoing request parameters. You're not clear on which view technology you're using, but as Facelets, Java EE's current default view technology, already implicitly uses UTF-8, I'll assume that you're still using legacy JSP. In that case, you need to set it by either

    <%@page pageEncoding="UTF-8"%>
    

    in top of every single JSP, or by

    <jsp-config>
        <jsp-property-group>
            <url-pattern>*.jsp</url-pattern>
            <page-encoding>UTF-8</page-encoding>
        </jsp-property-group>
    </jsp-config>
    

    in web.xml to apply it applicationwide.

  2. The HTTP request encoding which is used by the servletcontainer to parse and decode the incoming request parameters needs to be set to UTF-8. Again, you're not clear on which HTTP request method you're using. In case of POST, that would be the following call before you invoke any method which (implicitly) accesses the request body, such as getParameter().

    request.setCharacterEncoding("UTF-8");
    

    The best place for this call would be a servlet filter which is mapped on an URL pattern of /*. However, in case of GET (when the parameters are in the request URL instead of request body), it's not possible to control it programmatically. You'd need to configure it in the servletcontainer end. Again, you're not clear on which servletcontainer you're using. Based on your previously asked questions I see that you're using Tomcat or at least familiar with it. In that case, you need to open its /conf/server.xml file and edit the <Connector> element to add the URI encoding:

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

A possible third place which needs configuration would be the source where you're writing/storing/showing this data, such as the System.out.println() console or a database. But your own answer implies that this is already been set rightly.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • As you said: I´m using tomcat 6, the problem is using method GET, and finally, i´m using struts 1.3 and JSP. – Keetah May 03 '13 at 13:21