This article introduces the solution to Chinese garbled characters in jsp get Url requests. If you need it, refer to it.
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 ).