Detailed description of Jsp passed Chinese Parameters

Source: Internet
Author: User

Currently, you can pass Chinese parameters on the jsp page in either of the following ways:

URL, for example, http: // website/test1.jsp? Act = add & type = Orange & param = % 20D % 20B
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 the following two cases, we will provide a solution for correctly passing Chinese characters.

Method 1: URL
For example: http: // website/test1.jsp? Act = add & type = Orange & param = % 20D % 20B
In general, we seldom directly write the parameters in the URL as Chinese, such as "type = Orange" 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 );
%>

Another 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 ("Beijingers are 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 "));

Method 2: FORM
Note: We only discuss the Chinese situation in the form of <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>. 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 "));

In addition:
This article does not discuss URL encryption operations, but only the basic encoding for URL Chinese. In addition, you can use escape (), encodeURI (), and encodeURICompoent () in Javascript For URL encoding. This can be used as a reference solution for Chinese Javascript 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.