In JSP code, java.net. urlencoder. encode () is used to encode the Chinese characters to be passed. However, the received string is garbled. The most frustrating thing is that this code runs normally on some machines, but it cannot run normally on some machines. Because JSP and Web pages are not very familiar with each other, it takes a long time to find out the cause.
The Code does not use java.net. urlencoder, although java.net. urlencoder. encode () is used to encode the Chinese characters to be passed, but java.net. urlencoder. decode. In this case, some machines run normally, while others fail.
The following is a reference article for solving 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:
<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:
<% @ 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 ).