Several Methods to handle garbled Chinese parameters of URL links in Java are as follows:
Method 1:
Http://xxx.do? Ptname = 'I am a China'
String strptname = request. getparameter ("ptname ");
Strptname = new string (strptname. getbytes ("ISO-8859-1"), "UTF-8 ");
Method 2:
<% @ Page contenttype = "text/html; charset = gb2312" %>
<A href = "Ds. jsp? Url = <% = java.net. urlencoder. encode ("encode here", "gb2312") %> "> click here </a>
<%
// Request. setcharacterencoding ("GBK ");
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 );
}
%>
========================================
Public String chinatostring (string Str)
{
String S = STR;
Try
{
Byte tempb [] = S. getbytes ("ISO-8859-1 ");
S = new string (tempb );
Return S;
}
Catch (exception E)
{
Return S;
}
}
========================================================== ================
Function urlencode (SSTR)
{
Return escape (SSTR ).
Replace (// +/g, '% 2B ').
Replace (// "/g, '% 22 ').
Replace (// '/g,' % 27 ').
Replace (// G, '% 2f ');
}
Method 3:
If jstl is used, you can write an El function and call urlencoder. encode to encode it.
IE by default, the parameters following the URL are not encoded, but Tomat is decoded by ISO8859-1 by default, so the above error occurs. A good practice is:
1. Make sure to use UTF-8 encoding in URL parameters, the method can use JS function encodeuri (), or call the custom El function;
2. Set connector in server. XML to familiarize themselves with uriencoding = "UTF-8" to ensure that the decoding format is consistent with the encoding format;
Method 4:
<SCRIPT>
For (VAR I = 0; I <document. Links. length; I ++ ){
Document. Links [I]. href = encodeuri (document. Links [I]. href );
}
</SCRIPT>
In action, string S = request. getparameter ("S ");
S = new string (S. getbytes ("iso-8859-1"), "GBK ");
The above method is to gather the solutions described by some netizens.