Servlet Encoding Problem

I have a servlet that receives some parameter from the client and then does some work. And the parameter from the client is Chinese, so I often get some invalid characters in the servlet. For exmaple: If I introduce

http://localhost:8080/Servlet?q=中文&type=test

Then, in the servlet, the "type" parameter is correct (test), however the "q" parameter is incorrectly encoded, they become invalid characters that cannot be parsed.

However, if I enter the adder panel again, the URL will change to:

http://localhost:8080/Servlet?q=%D6%D0%CE%C4&type=test

Now my servlet will receive the correct "q" parameter.

What is the problem?

UPDATE

By the way, he speaks well when I submit a form with a message. When I post them in ajax, for example:

url="http://..q='中文',
xmlhttp.open("POST",url,true); 

Then the server side also receives invalid characters.

, % xx, .

http://.../q=中文 , http://.../q=%D6%D0%CE%C4 .

"http://www.google.com.hk/search?hl=zh-CN&newwindow=1&safe=strict&q=%E4%B8%AD%E6%96%87&btnG=Google+%E6%90%9C%E7%B4%A2&aq=f&aqi=&aql=&oq=&gs_rfai=" ? alt text

+3
4

, UTF-8 , UTF-8. , JSP, , :

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

, GET UTF-8, , servletcontainer . , , Tomcat: URIEncoding <Connector> /conf/server.xml UTF-8.

<Connector URIEncoding="UTF-8">

, POST, , HttpServletRequest POST UTF-8.

request.setCharacterEncoding("UTF-8");

, . A Filter - .

. :

+9

, ASCII, GET (.. URL-), . RFC 3986 UTF-8 , AFAIK . , , , - UTF-8!

, POST.

+1

, . , URL- , "" . : , Unicode , % xx.

, , . .

Another possibility is to use the POST method instead of GET.

0
source

Read this article in URL encoding format "www.blooberry.com/indexdot/html/topics/urlencoding.htm".

If you want, you can convert characters to hex or Base64 and put them in URL parameters.

I think it’s better to put them in the body (Post), then the URL (Get).

0
source

Source: https://habr.com/ru/post/1777288/


All Articles