Solution to Chinese garbled characters in jsp get Url requests

Source: Internet
Author: User

In jsp, post requests are generally not garbled. If the request is garbled, add the following sentence:

Solution 1

 

The code is as follows: Copy code
Request. setCharacterEncoding ("UTF-8 ");

For get requests, if the url contains non-Western European code, garbled characters are required. Processing method:

The code is as follows: Copy code

Request. setCharacterEncoding ("UTF-8 ");

// The request parameters are decomposed into byte arrays using ISO-8859-1, and then decoded into strings

The code is as follows: Copy code

String name = new String (request. getParameter ("name"). getBytes ("ISO-8859-1"), "UTF-8 ");

Solution 2

Java.net. URLEncoder. encode () transfer character encoding


Post the following article to solve this problem:

You can use java.net. URLEncoder. encode () to encode the Chinese characters to be passed.

A. Transcode the parameter before passing the parameter: java.net. URLEncoder. encode (param );
Use the statement java.net. URLDecoder. decode (param) to return the value to Chinese.
B. Find this section in your Tomcat directory --> conf directory --> server. xml:

The code is as follows: Copy code
<Connector
Port = "8080" maxThreads = "150" minSpareThreads = "25" maxSpareThreads = "75"
EnableLookups = "false" redirectPort = "8443" acceptCount = "100"
Debug = "0" connectionTimeout = "20000"
DisableUploadTimeout = "true"
<! -- Add this parameter here -->
URIEncoding = "gb2312"
/>

For example:

The code is as follows: Copy code

<% @ Page contentType = "text/html; charset = gb2312" %>

<A href = "ds. jsp? Url = <% = java.net. URLEncoder. encode ("encode here", "GB2312") %> "> click here </a>


<%
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 );
}

%>

If the character encoding is UTF-8, it can also be implemented. Or in this code segment, you can write only one parameter without writing the character encoding ).

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.