In jsp, post requests are generally not garbled. If the request is garbled, add the following sentence:
Solution 1
The code is as follows: |
Copy code |
Request. setCharacterEncoding ("UTF-8 "); |
For get requests, if the url contains non-Western European code, garbled characters are required. Processing method:
The code is as follows: |
Copy code |
Request. setCharacterEncoding ("UTF-8 "); |
// The request parameters are decomposed into byte arrays using ISO-8859-1, and then decoded into strings
The code is as follows: |
Copy code |
String name = new String (request. getParameter ("name"). getBytes ("ISO-8859-1"), "UTF-8 "); |
Solution 2
Java.net. URLEncoder. encode () transfer character encoding
Post the following article to solve this problem:
You can use java.net. URLEncoder. encode () to encode the Chinese characters to be passed.
A. Transcode the parameter before passing the parameter: java.net. URLEncoder. encode (param );
Use the statement java.net. URLDecoder. decode (param) to return the value to Chinese.
B. Find this section in your Tomcat directory --> conf directory --> server. xml:
The code is as follows: |
Copy code |
<Connector Port = "8080" maxThreads = "150" minSpareThreads = "25" maxSpareThreads = "75" EnableLookups = "false" redirectPort = "8443" acceptCount = "100" Debug = "0" connectionTimeout = "20000" DisableUploadTimeout = "true" <! -- Add this parameter here --> URIEncoding = "gb2312" /> |
For example:
The code is as follows: |
Copy code |
<% @ Page contentType = "text/html; charset = gb2312" %> <A href = "ds. jsp? Url = <% = java.net. URLEncoder. encode ("encode here", "GB2312") %> "> click here </a> <% If (request. getParameter ("url ")! = Null) {
Str = request. getParameter ("url "); Str = java.net. URLDecoder. decode (str, "GB2312 "); Str = new String (str. getBytes ("ISO-8859-1 ")); Out. print (str ); } %> |
If the character encoding is UTF-8, it can also be implemented. Or in this code segment, you can write only one parameter without writing the character encoding ).