jsp USES cookie to store Chinese sample sharing

  • 2020-06-23 01:42:12
  • OfStack

When I look at J2EE, I see in the book that when I use cookie to save information, all the examples in the book are English key-value pairs, and I wonder if there is one in Chinese. I tried one and it didn't work out. Without further ado, just code:

For example, addCookie.jsp has the following code:


<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title> increase 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 input localhost: 8080 / webDemo addCookie jsp? name= Test name to complete cookie addition.

Now it is time to take out cookie. getCookie. jsp code is as follows:


<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title> increase cookie</title>
</head>
<body>
<%
Cookie[] cookies = request.getCookies();// Take out the cookie
for(Cookie cc:cookies)// Go through and find the corresponding one cookie
{
    if(cc.getName().equals("username"))
    {
        out.println(cc.getValue());
    }
}
%>
</body>
</html>

But when in the address bar input localhost: 8080 / webDemo getCookie jsp found an error, cause this kind of situation because coding according to the provisions in RFC 2109, can contain only in Cookie ASCII coding.

So you can only set cookie with the Chinese code 1. The improved code is as follows:


<%
String name = request.getParameter("name");
byte[] rawName = name.getBytes("ISO-8859-1");
String strName = new String(rawName,"GB2312");// Gets the Chinese string form 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");// decoding 
        out.println(str); 
    }
}
%>

The other part of the problem is not so much that in the following code, someone did something different.


byte[] rawName = name.getBytes("ISO-8859-1");
String strName = new String(rawName,"GB2312");// Gets the Chinese string form of the parameter 
Cookie c = new Cookie("username",URLEncoder.encode(strName,"UTF-8"));

I searched a lot of information, only 1 code, such as: Cookie c = new Cookie("username",URLEncoder.encode(" Sun Wukong "," ES51en-8 ");

Put Chinese directly in the parameter position of encode method. It looks like name= ES56en.getParameter ("name"); Cookie c = new Cookie("username", ES64en. encode(name," UTF-8 ")); The code snippet, which seems to be correct, but in practice I found that it was confusing. I used Firefox and then I added two pieces of code, byte[] rawName = name. getBytes("ISO-8859-1");
String strName = new String(rawName,"GB2312"); Just did not produce disorderly code, specific why can produce such reason, I do not know, I do not know which god can explain 1.


Related articles: