JSP pass Chinese parameter detailed

Source: Internet
Author: User

There are two main ways to pass Chinese parameters in JSP pages:

URL way, for example: http://website/test1.jsp?act=add&type= Orange &param=%20d%20b
form, for example:
<form name=test mehtod= "POST" >
<input type=hidden name=text2 value= "Chinese" >
<input Type=text name=text1>
<input Type=submit value=submit>
</form>
The following two cases provide a solution for the correct delivery of Chinese

Way 1:url Way
For example: http://website/test1.jsp?act=add&type= Orange &param=%20d%20b
Generally speaking, we rarely write the parameters in Chinese directly in the URL, such as "Type= orange" in the example. If this happens, we just need to make a simple conversion on our receiving parameter page.

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);
%>

It is also more common to encode the Chinese characters in a URL into a character like 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 ("The 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");

Way 2:form Way
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.
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> situation. 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");

Another:
      This article does not discuss the operation of encrypting a URL, but only the basic encoding of the URL's Chinese. In addition, in JavaScript you can use Escape (), encodeURI (), encodeuricompoent () for the relevant URL encoding, can be used as a reference solution for JavaScript Chinese 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.