SSH environment JSP URL jump, with Chinese parameter garbled problem

Source: Internet
Author: User

The encoding format in the URL is iso-8859-1, and the encoding format is only converted in Chinese iso-8859-1

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

Method Two (recommended use):

<%@ page contenttype= "text/html;charset=gb2312"%>

<a href= "Ds.jsp?url=<%=java.net.urlencoder.encode (" coded is 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 Three:
If you use Jstl, you can write your own El function, call Urlencoder.encode to encode.

ie defaults to the URL after the parameter is not encoded sent, but Tomat by default is to press iso8859-1 for URL decoding, so the above error occurs. The good practice is to:
1, in the URL parameters to ensure that the UTF-8 code, the method can be used with the JS function encodeURI (), or call the custom El function;
2, set Server.xml Connector familiar with uriencoding= "UTF-8", to ensure that the decoding format and encoding format are unified;

Method Four:

<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");

SSH environment JSP URL jump, with Chinese parameter garbled problem

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.