Get way to submit Chinese coding problem and three ways to solve

Get way to submit Chinese coding problem and three ways to solve

Source: Internet
Author: User

Get way commits are very common in the web, and sometimes we have to submit the request using the Get method, but it is always frustrating for a container such as Tomcat to encode the get mode.

Let's talk about the process first:

Our content is sent using Get method, we will bring the content after the URL, the request sent by the browser is passed the URI code of the browser, sent to the server side, if it is struts2 will go through the interceptor URI decoding, and use "iso8859-1" to encode, Because the transfer process is binary.

There are three ways to solve the problem of Chinese coding

Assume:

Url:http://localhost:8080/test?name= Test

First, processing on the server

The content is "iso8859-1", using

String str = new String (Name.getbyte ("iso8859-1"), "Utf-8");

Get the correct value.

PS: This way there is a disadvantage, this way the server must be encoded by default using Iso8859-1, if I change the default iso8859-1 in Tomcat is Utf-8, then this method will not be effective.

Second, set in the Tomcat configuration file

In the Tomcat directory conf/server.xml, search "8080" port, if the port is changed to search by port number, after searching in this label hollow lattice input uriencoding= "UTF-8", it is iso8859-1 by default.

PS: The disadvantage of this way is obvious, change the server will be changed, sometimes forget is also very common things.

Third, manual coding more than once

Both the browser and the server are encoded and decoded, the support of Chinese is not very good, then can I convert the Chinese into English or digital transmission in advance?

Assume:

1, name= test, in the JS tag, use the encodeURI (param) method to first turn "test" into a name=%26%92 (fabricated) browser and then automatically URI encoding name=%2525%2592 (% of the URI encoded as%25);

OGNL expression, use <s:param name= "name" value= "%{@java. Net.URLEncoder.encode (name, ' Utf-8 ')}" > Uri-encode the value, Support for static methods Java.net.URLEncoder.encode () is also enabled in the Struts2 configuration file.

Struts.xml:name= "Struts.ognl.allowStaticMehtodAccess" value= "true"

2, this time the link get method submitted

3, the server to get name=%26%92, we re-use Uridecoder.decode (name, "Utf-8"); To decode so that you get the "test"

PS: This is not a better way to rely on Tomcat

    

Get way to submit Chinese coding problem and three ways to solve

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.