JSP page Chinese parameter transfer (GET and post method analysis) _jsp programming

Source: Internet
Author: User

In projects, we often encounter the need to pass Chinese characters in JSP page transitions. There are two main ways of doing this.

Url method

For example:

Http://website/test1.jsp?act=add&type= Apple &param=%20d%20b

Form mode

For example:

Copy Code code as follows:

﹤form name=test mehtod= "POST" ﹥
﹤input type=hidden name=text2 value= "Chinese" ﹥
﹤input Type=text Name=text1﹥
﹤input Type=submit Value=submit﹥
﹤/form﹥

We will provide solutions for the correct delivery of Chinese in both cases.

JSP page Chinese parameter transmission situation 1:url way

For example:

Http://website/test1.jsp?act=add&type= Apple &param=%20d%20b

Generally speaking, we rarely write the parameters in Chinese directly in the URL, such as the "Type= Apple" in the example. If this happens, we just need to make a simple conversion on our receiving parameter page.

JSP page Chinese parameter Transfer implementation Code test1.jsp: (main part)

Copy Code code 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 the Chinese characters in a URL into a character like type=%20d%20b.

JSP page Chinese parameter Delivery implementation code MYJSP1.JSP:

Copy Code code as follows:

﹤%@ page language= "java" import= "java.util.*" pageencoding= "gb2312"%﹥
﹤%@ page import= "java.net.*"%﹥
﹤a href= './myjsp2.jsp?act=﹤%=urlencoder.encode ("The Chinese are very good =-")%﹥ ' ﹥test﹤/a﹥

Code MYJSP2.JSP for the implementation of Chinese parameter transfer in JSP pages

Copy Code code 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");

JSP page Chinese parameter transmission situation 2:form way

Please note that we are only discussing the Chinese situation in ﹤form enctype= "application/x-www-form-urlencoded" ﹥ this form, because in enctype= "Multipart/form-data" You can also use this method to convert characters by parsing them, so don't repeat the discussion.

﹤form Method=post﹥ This is the simplest case.

JSP page Chinese parameter Delivery implementation code MYJSP1.JSP:

Copy Code code 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﹥

JSP page Chinese parameter Delivery implementation code MYJSP2.JSP:

Copy Code code as follows:

﹤%@ page language= "java" import= "java.util.*" pageencoding= "gb2312"%﹥
Request.setcharacterencoding ("gb2312");
Out.println (Request.getparameter ("act"));

Or

Copy Code code 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﹥ situation.

JSP page Chinese parameter Delivery implementation code MYJSP1.JSP:

Copy Code code 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﹥

JSP page Chinese parameter Delivery implementation code MYJSP2.JSP:

Copy Code code 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 about JSP page Chinese parameter pass Get and post method analysis, but this article does not discuss to encrypt the URL for the operation, only for URL Chinese basic encoding processing. In addition, you can use Escape (), encodeURI (), encodeuricompoent () for URL encoding in JavaScript, and as a reference solution for JavaScript Chinese encoding. It is hoped that this simple analysis of the JSP page's Chinese parameters passing get and post methods will help your programming design.

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.