Solution to garbled data passing between JSP forms

Source: Internet
Author: User

Reprinted from: http://read.newbooks.com.cn/info/169649.html

1,Article1

Recently encountered a problem: Use Get There is a problem with the method to pass Chinese characters. Post No problem.
The problem is described as follows:
<PRE> <a href = "usergroup. JSP ? Usergroupname = <% = usergroupname %> "> AA </a> </PRE>
Here usergroupname is Chinese
In usergroup. JSP The 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: pageencoding Post Function. Get When the method is submitted, you can see the submitted parameters in the address bar. This is becauseGet Method 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. While Post The 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 http Get The 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:
        1. <PRE>

<Form name = test mehtodd ="Post">

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

<Input type = text name = text1>

<Input type = submit value = submit>

</Form>

</PRE>

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.

CodeTest1.JSP(Main part) <PRE>

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

%> </PRE>

    • 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:

<PRE>

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

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

</PRE>

Code myjsp2.JSP:

<PRE>

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

</PRE>

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:

<PRE>

<% @ 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>

</PRE>

Code myjsp2.JSP:

<PRE>

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

Request. setcharacterencoding ("gb2312 ");

Out. println (request. getparameter ("act"); </PRE>

Or <PRE>

<% @ 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> </PRE>.

Code myjsp1.JSP:

<PRE>

<% @ 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> </PRE>

Code myjsp2.JSP:

<PRE>

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

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

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

</PRE>

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.