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.