Solutions to Chinese garbled Characters During get/post

Source: Internet
Author: User

1. Article 1

Recently encountered a problem: UseGetThere is a problem with the method to pass Chinese characters.PostNo problem.
The problem is described as follows:
<A href = "usergroup.JSP? Usergroupname = <% = usergroupname %> "> AA </a>
Here usergroupname is Chinese

In usergroup.JSPThe usergroupname obtained on the page is garbled.
Each page is also described in <% @ page Language = "Java" pageencoding = "GBK" %>.

After searching for the Internet, you will find out: pageencodingPostFunction.GetWhen the method is submitted, you can see the submitted parameters in the address bar. This is becauseGetMethod transmission is submitted as the packet header, and pageencoding has no effect on the packet header, so the garbled problem occurs only after coding according to the iso8859-1. WhilePostThe content of the form is submitted, and pageencoding specifies its encoding, so it will be passed according to the specified encoding.

The problem is clear, and we will solve it below:

Because the servletrequest. setcharacterencoding method in Tomcat Servlet implementation does not decode the content of the htp packet header
Use httpGetThe data submitted by the method cannot be decoded correctly. The solution is to modify the configuration of the server. xml file for the HTTP protocol
Connector configuration, with the uriencoding = "GBK" attribute added, after the configuration is complete, the "possible" content is
<Connection Port = "8080"
Maxthreads = "150" minsparethreads = "25" maxsparethreads = "75"
Enablelookups = "false" redirectport = "8443" acceptcount = "100"
DEBUG = "0" connectiontimeout = "20000"
Disableuploadtimeout = "true"Uriencoding = "GBK"/>
Success!

2. Article 2

Submitting a form, method ="Get"Is the default value. The URL is submitted in the format of http: // localhost: 8081/test.JSP? Username = bebe0453 & Password = 082628 & submit = % C8 % B7 % EF % BF % BD

And method ="Post", It is an implicit commit, and no parameters will appear in the browser address bar.

PostMore confidential, and larger capacity submitted in the past, generally usedPostSubmit.

 

3. Article 3

In projects, we often encounter the needJSPTransfer Chinese Characters During page switching. There are two main methods.

  1. URL, for example, http: // website/test1.JSP? Act = add & type = apple & Param = % 20d % 20b
  2. Form method, for example:

<Form name = test mehtodd ="Post">

<Input type = hidden name = text2 value = "">

<Input type = text name = text1>

<Input type = submit value = submit>

</Form>

In these two cases, we will provide a correct solution for passing Chinese characters.

Case 1: URL Method

For example, http: // website/test1.JSP? Act = add & type = apple & Param = % 20d % 20b

  • In general, we seldom directly write the parameters in the URL as Chinese, such as "type = Apple" in the example. In this case, we only need to make a simple conversion on the page where we receive parameters.

Code test1.JSP(Main part)

<% @ Page Language = "Java" Import = "Java. util. *" pageencoding = "gb2312" %>

<%

String type = request. getparameter ("type ");

String result = new string (type. getbytes ("iso-8859-1"), "gb2312 ");

Out. println (result );

%>

  • The more common practice is to encode Chinese characters in the URL and convert them into characters such as type = % 20d % 20b.

Code myjsp1.JSP:

<% @ Page Language = "Java" Import = "Java. util. *" pageencoding = "gb2312" %>
<% @ Page import = "java.net. *" %>

<A href = './myjsp2.JSP? Act = <% = urlencoder. encode ("Chinese very good =-") %> '> test </a>

Code myjsp2.JSP:

<% @ Page Language = "Java" Import = "Java. util. *" pageencoding = "gb2312" %>
<% @ Page import = "java.net. *" %>

String tempval = urldecoder. Decode (request. getparameter ("act "));
Out. println (new string (tempval. getbytes ("ISO-8859-1"), "gb2312 "));

Case 2: Form Mode

Please note that we only discuss the Chinese version of this form in <form enctype = "application/X-WWW-form-urlencoded">, since enctype = "multipart/form-Data" can be parsed to Chinese characters, this method can also be used for character conversion, so we will not discuss it again.

  • <Form method =Post> This is the simplest case.

 Code myjsp1.JSP:

<% @ Page Language = "Java" Import = "Java. util. *" pageencoding = "gb2312" %>

<Form action = "./myjsp2.JSP"Method ="Post"Enctype =" application/X-WWW-form-urlencoded ">
<Input type = hidden name = act value = action/>
<Input type = submit value = OK>
</Form>

Code myjsp2.JSP:

<% @ Page Language = "Java" Import = "Java. util. *" pageencoding = "gb2312" %>

Request. setcharacterencoding ("gb2312 ");

Out. println (request. getparameter ("act "));

Or

<% @ Page Language = "Java" Import = "Java. util. *" pageencoding = "gb2312" %>

String tempval = request. getparameter ("act ");

Out. println (new string (tempval. getbytes ("ISO-8859-1"), "gb2312 "));

  • <Form method =Get> Situation.

Code myjsp1.JSP:

<% @ Page Language = "Java" Import = "Java. util. *" pageencoding = "gb2312" %>

<Form action = "./myjsp2.JSP"Method ="Get"Enctype =" application/X-WWW-form-urlencoded ">
<Input type = hidden name = act value = action/>
<Input type = submit value = OK>
</Form>

Code myjsp2.JSP:

<% @ Page Language = "Java" Import = "Java. util. *" pageencoding = "gb2312" %>

String tempval = request. getparameter ("act ");

Out. println (new string (tempval. getbytes ("ISO-8859-1"), "gb2312 "));

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.