Javascript uses URL to pass Chinese parameter garbled characters to JSP pages. solution:

Source: Internet
Author: User

Solution: encode the Chinese parameter twice before passing the parameter. After obtaining the parameter on the JSP page, decode the Chinese parameter once. The Chinese parameter will not become garbled!

Example:

<% @ Page Language = "Java" contenttype = "text/html; charset = UTF-8"
Pageencoding = "UTF-8" %>
<% @ Page import = "java.net. *" %>
<%
String str0 = "";
String str1 = "";
If (request. getparameter ("param0 ")! = NULL ){
Str0 = request. getparameter ("param0"); // get Chinese parameters directly
}
Try {
If (request. getparameter ("param1 ")! = NULL ){
Str1 =Urldecoder. Decode ( Request. getparameter ("param1 "), " UTF-8 ") ; // Decodes Chinese Parameters
}
} Catch (exception e ){
E. printstacktrace ();
}
%>
<! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en">
<HTML>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8">
<SCRIPT type = "text/JavaScript">
VaR STR = "hello ";
Function test0 (){
Window. Location = "test. jsp? Param0 = "+ STR; // directly pass Chinese Parameters
}
Function test1 (){
Window. Location = "test. jsp? Param1 = "+ Encodeuri ( Encodeuri ( Str ) ) ; // Encode Chinese parameters and pass them again
}
</SCRIPT>
</Head>
<Body>
<Input value = <% = str0 %>
<Input type = "button" value = "garbled" onclick = "test0 ()"> <br>
<Input value = <% = str1 %>
<Input type = "button" value = "normal" onclick = "test1 ()">
</Body>
</Html>

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.