Analysis of get and post methods for passing Chinese parameters on JSP pages

Source: Internet
Author: User

In projects, we often encounter the need to pass Chinese Characters in JSP page switching. There are two main methods.

◆ URL Method

For example:

 
 
  1. Http: // website/test1.jsp?Act=Add&Type= Apple &Param= % 20D % 20B

◆ FORM Mode

For example:

 
 
  1. <FormName=TestMehtodd="Post">
  2. <InputType=Hidden Name=Text2 Value="Chinese">
  3. <InputType=Text Name=Text1>
  4. <InputType=Submit Value=Submit>
  5. </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:

 
 
  1. 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)

 
 
  1. ﹤%@ page language="java" import="java.util.*" pageEncoding="gb2312"%﹥  
  2. ﹤%  
  3. String type = request.getParameter("type");  
  4. String result = new String(type.getBytes("iso-8859-1"), "gb2312");  
  5. out.println(result);     
  6. %﹥  
  7.  

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:

 
 
  1. <%@ PageLanguage="Java" Import="Java. util .*" PageEncoding="Gb2312"%>
  2. <%@ PageImport="Java.net .*"%>
  3. <Href='./MyJsp2.jsp? Act = <%= URLEncoder. encode ("Chinese very good =-") %>'> Test </a>
  4.  

Code for passing Chinese parameters on JSP pages MyJsp2.jsp

 
 
  1. ﹤%@ page language="java" import="java.util.*" pageEncoding="gb2312"%﹥  
  2. ﹤%@ page import="java.net.*" %﹥  
  3.  
  4. String tempVal = URLDecoder.decode(request.getParameter("act"));  
  5. out.println(new String(tempVal.getBytes("ISO-8859-1"), "gb2312"));  
  6.  

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:

 
 
  1. <%@ PageLanguage="Java" Import="Java. util .*" PageEncoding="Gb2312"%>
  2. <FormAction="./MyJsp2.jsp" Method="Post" Enctype="Application/x-www-form-urlencoded">
  3. <InputType=Hidden Name=Act Value= Action/>
  4. <InputType=Submit Value=OK>
  5. </Form>
  6.  

Code for passing Chinese parameters on JSP pages MyJsp2.jsp:

 
 
  1. ﹤%@ page language="java" import="java.util.*" pageEncoding="gb2312"%﹥  
  2. request.setCharacterEncoding("gb2312");  
  3. out.println(request.getParameter("act"));  
  4.  

Or

 
 
  1. ﹤%@ page language="java" import="java.util.*" pageEncoding="gb2312"%﹥  
  2. String tempVal = request.getParameter("act");  
  3. out.println(new String(tempVal.getBytes("ISO-8859-1"), "gb2312")); 

◆ <Form method = get>.

Code for passing Chinese parameters on JSP pages MyJsp1.jsp:

 
 
  1. <%@ PageLanguage="Java" Import="Java. util .*" PageEncoding="Gb2312"%>
  2. <FormAction="./MyJsp2.jsp" Method="Get" Enctype="Application/x-www-form-urlencoded">
  3. <InputType=Hidden Name=Act Value= Action/>
  4. <InputType=Submit Value=OK>
  5. </Form>
  6.  

Code for passing Chinese parameters on JSP pages MyJsp2.jsp:

 
 
  1. ﹤%@ page language="java" import="java.util.*" pageEncoding="gb2312"%﹥  
  2. String tempVal = request.getParameter("act");  
  3. 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.

  1. JSP entry-level website environment setup steps
  2. Analysis of Script Programming in JSP development
  3. How to export Oracle Data Tables Using JSP
  4. Implement page Jump in JSP
  5. What is JSP and comparison with Servlet?

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.