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.