简体   繁体   中英

Java handle special characters request parameters

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.

索取详细资料

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.

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"); .

还将utf-8编码(而不是iso-8859 getBytes ()使用getBytes ()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM