Summary of JSP's processing methods for Chinese garbled codes in URL links

  • 2021-10-13 08:20:06
  • OfStack

By default, IE is sent without encoding the parameters after URL, but Tomat is encoded by ISO8859-1 by default, so an error will occur.

Method 1:

Encode the URL link twice:


<a onclick="javascript:window.open(encodeURI(encodeURI('./DispatchAction.do?efFormEname=FKRY0001&code_type= Chinese parameter ')))"> Test </a>

Or encode the parameters twice separately:


var code_type = " Chinese parameter ";
code_type = encodeURI(code_type); 
code_type = encodeURI(code_type);
window.open("./DispatchAction.do?efFormEname=FKRY0001&code_type="+code_type);
Service : 
String code_type = request.getParameter("code_type");
// This sentence 1 Be sure to write, because if you don't write, the code is %E5%A6%88%
code_type = java.net.URLDecoder.decode(code_type,"UTF-8");

Someone asked why the string should be encoded twice on the client side.

If you can't specify what encoding rules the container uses to decode submitted parameters because of the project's needs, for example, if you need to receive parameter content from different pages without encoding. (Or maybe the developer is confused by this complicated thing and doesn't know how to do the job of receiving parameters correctly.)

At this time, encoding the parameters twice on the client side can effectively avoid the thorny problem of "submitting multi-byte characters".

Because of the first encoding, your parameter content does not have multi-byte characters, and becomes a pure Ascii string. (Let's call the result of the first edition [STR_ENC1]. [STR_ENC1] does not have multibyte characters.)

After reprogramming once, the container automatically resolves once when submitting and receiving (the container automatically resolves once, no matter according to GBK, UTF-8 or ISO-8859-1, it can get [STR_ENC1] correctly)

Then, implement decodeURIComponent once in the program (java. net. URLDecoder. decode (***, "UTF-8")) to get the original value of the parameter you want to submit.

To put it simply, the Tomcat server will automatically help you do URLDecode once, and with the URLDecode written by yourself in the Service code, there are two Decode. Since you need Decode twice, you need Encode twice. Maybe you will ask, simply only Encode1 times, and then not Decode in java code, hehe, this is not good either, which is actually why Encode should be carried out twice.

Method 2: (IE8 is not supported after testing)


http://xxx.do?ptname= Chinese parameter 
String strPtname = request.getParameter("ptname");
strPtname = new String(strPtname.getBytes("ISO-8859-1"), "UTF-8");

Method 3:


<%@ page contentType="text/html;charset=gb2312" %>
<a href="ds.jsp?url=<%=java.net.URLEncoder.encode(" Coded 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);
}
%>

Method 4:
Set server in Tomcat. Connector in xml is familiar with URIEncoding= "UTF-8" to ensure decoding format and encoding format unification 1.


Related articles: