A summary of the Chinese garbled processing method of the URL link in JSP _jsp programming

Source: Internet
Author: User
Tags tomcat server

ie defaults to the URL after the parameter is not encoded, but Tomat default is to press iso8859-1 for URL encoding, so the error.

Method One:

To encode a URL link two times:

<a onclick= "Javascript:window.open (encodeURI encodeURI ('./dispatchaction.do?efformename=fkry0001&code_ type= Chinese parameter ')] "> Test </a>

Or the parameter is encoded individually two times:

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 must be written, because if not write code is%e5%a6%88%
code_type = Java.net.URLDecoder.decode (Code_type, "UTF-8");

Why do you want to repeat the string code two times on the client?

If you cannot specify what encoding rules the container uses to decode the submitted parameters because of the project needs, for example, you need to receive parameter content that is not encoded from different pages. (or developers are confused by this somewhat complicated thing and don't know how to do the job of receiving parameters correctly)

At this time, the parameters are encoded two times on the client, which can effectively avoid the "submit multibyte characters" of this thorny problem.

Because the first time you code, your parameter content will not have multibyte characters, and become a pure Ascii string. (The result of the first time is called [str_enc1]. [STR_ENC1] is not a multibyte character

Once again, submit, receive the container automatically solution once (the container automatically solution this time, whether it is by GBK or UTF-8 or iso-8859-1 are good, can be the right to get [STR_ENC1])

Then, implement a decodeuricomponent in the program (Java.net.URLDecoder.decode (* * *, "UTF-8") is usually used in Java to get the original value of the parameter you want to commit.

In short, the Tomcat server will automatically help you do a urldecode, plus your own in the service code written in the UrlDecode, altogether is two decode. Since two times to decode, of course, it will take two times encode. Perhaps you will ask, simply encode once, and then in the Java code does not decode, oh, this is not good, this is actually why to do two times encode reasons.

Method Two: (IE8 is not supported by test)

Http://xxx.do?ptname= Chinese parameter
String strptname = Request.getparameter ("Ptname");
Strptname = new String (strptname.getbytes ("iso-8859-1"), "UTF-8");

Method Three:

<%@ page contenttype= "text/html;charset=gb2312"%>
<a href= "ds.jsp?url=<%= Java.net.URLEncoder.encode ("Encoded 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 Four:
The connector in the Setup Server.xml in Tomcat is familiar with uriencoding= "UTF-8", ensuring that the decoding format is consistent with the encoding format.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.