Transcoding of parameters for transferring Chinese characters between pages in jsp

Source: Internet
Author: User

If you directly use Chinese characters by passing parameters in the url address bar, some may find that garbled characters have been received or the search engine has become messy in the past, next let's take a look at the correct method for cross-page Chinese Character transfer parameter transcoding in jsp.

Transcoding: a. href = "./showCont. jsp? Tcontent = "+ encodeURI (tcontent ));

Decoding: java.net. URLDecoder. decode (String) request. getParameter ("tcontent"), "UTF-8 ");


Example

A. jsp source code

The Code is as follows: Copy code

<% @ Page contentType = "text/html; charset = gb2312" language = "java" import = "java. SQL. *" errorPage = "" %>
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312"/>
<Title> untitled document </title>
</Head>

<Body>
<%
String str_test = "";
%>
<Form method = post action = "B. jsp? Test = <% = java.net. URLEncoder. encode (str_test) %> ">
<Input type = "submit" value = "Submit" name = "submit">
</Form>

</Body>
</Html>


B. jsp source code

The Code is as follows: Copy code

<% @ Page contentType = "text/html; charset = gb2312" language = "java" import = "java. SQL. *" errorPage = "" %>
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312"/>
<Title> untitled document </title>
</Head>

<Body>

<%
String str = new String (request. getParameter ("test"). getBytes ("ISO8859_1 "));
%>
<BR>
<% = Str %>
</Body>
</Html>

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.