In projects, we often encounter the need to pass Chinese Characters in JSP page switching. There are two main methods.
◆ URL Method
For example:
Http: // website/test1.jsp? Act = add & type = apple & param = % 20D % 20B
◆ FORM Mode
For example:
Copy codeThe Code is as follows:
<Form name = test mehtodd = "post">
<Input type = hidden name = text2 value = "Chinese">
<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.
Pass Chinese parameters on JSP pages 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)
Copy codeThe Code is as follows:
<% @ 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 for passing Chinese parameters on JSP pages MyJsp1.jsp:
Copy codeThe Code is as follows:
<% @ 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 for passing Chinese parameters on JSP pages MyJsp2.jsp
Copy codeThe Code is as follows:
<% @ 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 "));
Pass Chinese parameters on JSP pages 2: FORM Mode
Please note that we only discuss the Chinese version 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 for passing Chinese parameters on JSP pages MyJsp1.jsp:
Copy codeThe Code is as follows:
<% @ 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 for passing Chinese parameters on JSP pages MyJsp2.jsp:
Copy codeThe Code is as follows:
<% @ Page language = "java" import = "java. util. *" pageEncoding = "gb2312" %>
Request. setCharacterEncoding ("gb2312 ");
Out. println (request. getParameter ("act "));
Or
Copy codeThe Code is as follows:
<% @ 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 for passing Chinese parameters on JSP pages MyJsp1.jsp:
Copy codeThe Code is as follows:
<% @ 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 for passing Chinese parameters on JSP pages MyJsp2.jsp:
Copy codeThe Code is as follows:
<% @ Page language = "java" import = "java. util. *" pageEncoding = "gb2312" %>
String tempVal = request. getParameter ("act ");
Out. println (new String (tempVal. getBytes ("ISO-8859-1"), "gb2312 "));
The above is the analysis of the get and post methods for passing Chinese parameters on the JSP page. However, 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. It is hoped that the analysis of the get and post Methods passed to the Chinese parameters of JSP pages will be helpful for your programming design.