Reprinted from Der Stan: get post garbled solution

The full text is reproduced from Der Stan


Because it involves a function of the project, we use the hyperlink a tag as a page break to perform a fuzzy query function. During the use process, we use the form form post to submit Chinese data to enter the servle interface. There is no problem in using Chinese query for the first time, but When I pressed the page break, I found that the Chinese data could not be transferred to the background again, so I simply used the method of adding a value at the end of the address bar, and then jumped directly to the Servlet interface, because the hyperlink uses the GET method to jump by default, a three-piece set It has an effect on POST submission, but has no effect on GET submission, so the Servlet receives garbled characters when it accepts the Chinese value passed from the address bar;

Three piece set:

response.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
request.setCharacterEncoding("utf-8");

I used the method of URLEncoder.encode((request.getAttribute("name")+""), "utf-8") for the tag value of the jsp page again, which had a little effect, and the value passed in was not garbled. But a question mark? , the browser's default jump code is ISO-8859-1, not utf-8, so this method is used here, but the problem is still not solved, check the relevant information and find that the fastest shortcut is to modify the Tomcat server. The configuration of xml, because there is less relevant information, how to modify, where to modify, what to modify, and less online information, so I will share it here. It is in the conf directory by default, and only two lines of code URIEncoding= need to be added under the Connector tag. "UTF-8"  and   useBodyEncodingForURI="true" are enough, these two codes will be changed to UTF-8 by default in the encoding submitted by GET

 <Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" 
               URIEncoding="UTF-8" 
        useBodyEncodingForURI="true"/
>
    <!-- A "Connector" using the shared thread pool-->
    <!--
    <Connector executor="tomcatThreadPool"
               port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324549006&siteId=291194637
Recommended