How to handle garbled Chinese parameters of URL links in Java

Source: Internet
Author: User

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.

 

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.