1

Im working on a Flex-Java (Client-Server) application and Im stuck on a problem. Im sending to the server path parameters and receiving them on a JSP, sometimes, those parameters have special characters like áéíóú or ñ. I have what I think is a common problem, those special characters are being wrong received on my server.

I also checked out another questions having the same isuue like this one or this other.

I have already tried to use request.setCharacterEncoding("UTF-8") for receiving the params (answer from first question).

I also tried checking the configuration of my server (Apache Tomcat 7.0.34.0) on the server.xml file and Im sure that the node with port 8080 has the URIEncoding="UTF-8" attribute (answer from second question).

I even tried reading the param like:

String paramTest1 = (new String(request.getParameter("paramTest").getBytes("ISO-8859-1"),"UTF-8")); or String paramTest2 = (new String(request.getParameter("paramTest").getBytes("UTF-8")));

Debugging my app I realized that the URI was well-received, the problem is on the moment I read the parameters from the request.

Request detail

Currently I receive the parameters by the tradicional way, the code of my JSP:

<%@page contentType="text/html" pageEncoding="UTF-8" language = "java"%>
<%
request.setCharacterEncoding("UTF-8");
String paramTest = request.getParameter("paramTest");
String paramTest1 = (new String(request.getParameter("paramTest").getBytes("ISO-8859-1"),"UTF-8"));
String paramTest2 = (new String(request.getParameter("paramTest").getBytes("UTF-8")));
%>

As in the image can be apreciated Im sending the String "1234acentoáéíóúeñe" for the paramTest parameter. The values of my vars obtaining the parameteres are:

paramTest = 1234acento�����
paramTest1 = 1234acento?????
paramTest2 = 1234acento�����

And I need to receive exactly the same characters that Im sending.

Dont know if it is important to say but on the Flex side, for the URLRequest object Im specifying a URLRequestHeader object ("Content-Type", "text/html;charset=UTF-8") for the requestHeaders Araray. That doesn't hel for my purpose.

Any comment or answer is well appreciated.

Thanks.

Community
  • 1
  • 1
Hugo Allexis Cardona
  • 1,382
  • 19
  • 25

2 Answers2

1

Maybe it's too-old technology but could be usefull for people who still write code on as4 (flex).

I realized that the special characters couldn't be well encoded only when the request I send from flex was a file request (for uploading a file), if the characters where sent with an ordinary request (POST parameters request) then the request.setCharacterEncoding("UTF-8"); was enough for encoding and reading the parameters correctly.

So, when the request is a fileRequest, the parameters has to be sent as URI-parameters on the URLRequest parameter of the FileReference and thats why the parameters look good on the URI but couldn't be read on the server.

Ok ok, too much talk, so, the code for encoding the quoted characters on the client side is:

public static function encodeQuotes(s:String):String {
    return s.replace("á", "%C3%A1").replace("é", "%C3%A9").replace("í", "%C3%AD").replace("ó", "%C3%B3").replace("ú", "%C3%BA").replace("ñ", "%C3%B1").
        replace("Á", "%C3%81").replace("É", "%C3%89").replace("Í", "%C3%8D").replace("Ó", "%C3%93").replace("Ú", "%C3%9A").replace("Ñ", "%C3%91");
}

Then you can read the parameters on the server side with the same request.setCharacterEncoding("UTF-8");.

Hugo Allexis Cardona
  • 1,382
  • 19
  • 25
0

Use getBytes () also with utf-8 encoding instead of iso-8859.

Bimalesh Jha
  • 1,464
  • 9
  • 17
  • I forgot to include it on my answer but I also tried using `String paramTest3 = (new String(request.getParameter("paramTest").getBytes("UTF-8")));` and it results on the String `1234acento�����` for the same parametter. Thanks for replying. – Hugo Allexis Cardona Jul 16 '13 at 03:09
  • @HugoAllexisCardona you have to use UTF-8 at both places: like `new String(req.getParameter("paramTest").getBytes("UTF-8"),"UTF-8");` – Bimalesh Jha Jul 16 '13 at 15:01
  • I tried it, also result on `1234acento�����` value for the same param. Any other recomendation? – Hugo Allexis Cardona Jul 16 '13 at 15:20
  • where are you seeing the result "1234acento?????" To display on browser, e.g. Firefox set the correct encoding by View -> Character Encoding -> UTF-8 – Bimalesh Jha Jul 16 '13 at 16:15
  • I'm seeing the result on the NetBeans debugger, creating a watch expression for those variables storing the param. – Hugo Allexis Cardona Jul 17 '13 at 00:09
  • not sure about netneans, but, in Eclipse, there is a way to set the character encoding to utf-8 rather than using the default OS encoding. Ultimately, your application will be invoked from browser, hence, it is a good idea to test with FireFox or IE, with proper charset, too. – Bimalesh Jha Jul 18 '13 at 03:48