Jsp uses cookies to store Chinese examples

Source: Internet
Author: User

When I read J2EE, I saw the use of cookies to store information. The examples in this book are all English key-value pairs. I just want to know if Chinese is the same? I tried it again. If you don't talk much about it, go directly to the Code:

For example, the addCookie. jsp code is as follows:
Copy codeThe Code is as follows:
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Title> Add cookie </title>
</Head>
<Body>
<%
String name = request. getParameter ("name ");
Cookie c = new Cookie ("username", name );
C. setMaxAge (3600 );
Response. addCookie (c); // Add cookie
%>
</Body>
</Html>

In the address bar, enter localhost: 8080/webDemo/addCookie. jsp? Name = test name to complete cookie addition.

The following code retrieves the cookie. The getCookie. jsp code is as follows:
Copy codeThe Code is as follows:
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Title> Add cookie </title>
</Head>
<Body>
<%
Cookie [] cookies = request. getCookies (); // retrieve the cookie
For (Cookie cc: cookies) // traverse to find the corresponding cookie
{
If (cc. getName (). equals ("username "))
{
Out. println (cc. getValue ());
}
}
%>
</Body>
</Html>

However, when localhost: 8080/webDemo/getCookie. jsp is entered in the address bar, an error is reported. This is due to the encoding reason. According to RFC 2109, cookies can only contain ASCII encoding.

You can only encode Chinese characters when setting cookies. The improved code is as follows:
Copy codeThe Code is as follows:
<%
String name = request. getParameter ("name ");
Byte [] rawName = name. getBytes ("ISO-8859-1 ");
String strName = new String (rawName, "GB2312"); // obtain the Chinese String format of the Parameter
Cookie c = new Cookie ("username", URLEncoder. encode (strName, "UTF-8 "));
C. setMaxAge (3600 );
Response. addCookie (c );
%>
<%
Cookie [] cookies = request. getCookies ();
For (Cookie cc: cookies)
{
If (cc. getName (). equals ("username "))
{
String str = URLDecoder. decode (cc. getValue (), "UTF-8"); // decode
Out. println (str );
}
}
%>

It is not a problem in other places, that is, in the following code, some people do different.
Copy codeThe Code is as follows:
Byte [] rawName = name. getBytes ("ISO-8859-1 ");
String strName = new String (rawName, "GB2312"); // obtain the Chinese String format of the Parameter
Cookie c = new Cookie ("username", URLEncoder. encode (strName, "UTF-8 "));

I found a lot of information, there is only a piece of code, such as: Cookie c = new Cookie ("username", URLEncoder. encode ("Sun Wukong", "UTF-8 "));

Place the Chinese character directly in the parameter location of the encode method, and it looks like it can be directly in name = request. getParameter ("name"); then the above Cookie c = new Cookie ("username", URLEncoder. encode (name, "UTF-8"); Code segment, it looks like nothing is wrong, but I found in practice will produce garbled code, I use Firefox browser, then I added two pieces of code: byte [] rawName = name. getBytes ("ISO-8859-1 ");
String strName = new String (rawName, "GB2312"); no garbled code is generated. I don't know why this problem occurs. I don't know why.

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.