Use java.net. urlencoder. encode () to transmit character encoding in JSP

Source: Internet
Author: User

In JSP code, java.net. urlencoder. encode () is used to encode the Chinese characters to be passed. However, the received string is garbled. The most frustrating thing is that this code runs normally on some machines, but it cannot run normally on some machines. Because JSP and Web pages are not very familiar with each other, it takes a long time to find out the cause.

The Code does not use java.net. urlencoder, although java.net. urlencoder. encode () is used to encode the Chinese characters to be passed, but java.net. urlencoder. decode. In this case, some machines run normally, while others fail.

 

The following is a reference article for solving 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:
      <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:
<% @ 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.