JSP uses cookies to store Chinese sample sharing

Source: Internet
Author: User
Tags xmlns

  This article mainly describes the JSP using cookies to store Chinese samples, you need friends can refer to the following

Look at the time, see the book on the use of cookies to save information, see the book cited examples are English key value pairs, I think Chinese is not the same? A try is not the same. Nonsense not much to say, directly on the code:   For example, addcookie.jsp code is as follows:     code 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>     Enter localhost:8080/webdemo/in the address bar Addcookie.jsp?name= the test name to complete the cookie Add.   The following is the removal of cookies, getcookie.jsp code as follows:     code as follows: <html xmlns= "http://www.w3.org/1999/xhtml" > < head>     <title> add cookie</title> </head> <body> <% cookie[] cookies = Request.getcookies ()///Fetch cookie for (cookie cc:cookies)/traversal find the corresponding cookie {    if (Cc.getname (). Equals (" Username "))     {        OUT.PRINTLN (Cc.getvalue ());    }}%> </body> </html>    But when you enter localhost:8080/webdemo/getcookie.jsp in the Address bar, you find an error, which is because the encoding is based on the requirements in RFC 2109, Only ASCII encoding can be included in the cookie.   Then it's OK to encode the Chinese when you set up cookies. The improved code is as follows:     code is as follows: <% String name = Request.getparameter ("name"); byte[] Rawname = name.getbytes ("iso-8859-1"); String strName = new String (Rawname, "GB2312"),//Get parameter in Chinese string form 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"))     {        STR ing str = urldecoder.decode (Cc.getvalue (), "UTF-8");/decoding         OUT.PRINTLN (str);     }%>     Other local problems are not the same as in the following code, some people do different things. The code is as follows: byte[] Rawname = name.getbytes ("iso-8859-1"); String strName = new String (Rawname, "GB2312"),//Get parameter in Chinese string form Cookie c = new Cookie ("username", Urlencoder.encode (strName , "UTF-8"));   I searched a lot of information,There is only a piece of code, such as: cookie C = new Cookie ("username", Urlencoder.encode ("Monkey King", "UTF-8"));   Place Chinese directly in the parameter position of the Encode method, it appears that it can be directly in Name=request.getparameter ("name"), and then call the cookie c = new Cookie ("username") above. Urlencoder.encode (name, "UTF-8")); code snippet, it looks like there is no wrong, but I found in practice will produce garbled, I use Firefox browser, and then I added two pieces of code, is: byte[] Rawname = Name.getbytes ("iso-8859-1"); String strName = new String (Rawname, "GB2312"); there is no garbled, specific why the reason for this, I do not know, I do not understand which great God can explain.  
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.