Cookie operation of 04_web Foundation ()

Source: Internet
Author: User

30, 31, 32, cookie operation:

1): Create a cookie and set up shared data:
Cookie C = new Cookie (String name,string value);
Cookie C = new Cookie ("Curreentname", "Wuji");
2): Put the cookie into the response, the shared data of the cookie is passed to the browser and saved by the browser.
Response object. Addcookie (c);
3): Obtain data from cookies and cookies. (obtained from the request)

4): The name and value of the cookie do not support Chinese.
Solution: Encode and decode the Chinese language.
Put
String msg= "Hello";
String encode = Urlencoder.encode (msg, "UTF-8");
System.out.println (encode);
Cookie C2 = new Cookie ("CurreentName2", encode);
Take:
String name = Cookie.getname ();
String value = Cookie.getvalue ();
String decode = Urldecoder.decode (value, "UTF-8");

5): Change the value of the specified name of the cookie.
Mode 1: Gets the modified cookie object according to the name, just call the SetValue method.
Mode 2: Recreate a cookie with the same name.

Note: After modifying, you call the Response object. Addcookie (c);

6): The life cycle of the cookie: (How long the shared data in the cookie can be retained)
By default: Closing the browser cookie is lost.
Setmaxage (int seconds) of the cookie object: How long the cookie can be set to survive.
Seconds>0: How many seconds can be survived.
seconds<0: stored in the browser process, closed browser cookie is lost.
Seconds=0: Delete cookies.
7): Delete cookies:
Cookie object. setmaxage (0);
8): The cookie is defective. (emphasis in the focus)
1&gt: Chinese handling trouble.
2&gt: Multiple people sharing the same computer, information is not secure.
3>: A cookie can store only one simple type of data.
To store, account, password, mail, etc. at the same time.
The main reason is that the value of a cookie can only store string and cannot save object.
4>:cookie size and quantity limits:
The cookie size limit is within 4KB;
* A server holds a maximum of 20 cookies on a client;
* A browser can hold up to 300 cookies;
The 5>:cookie principle: Store shared data in a browser.
Each request, and then bring the shared data to the server.

Code:

1  PackageCom.day07.web.demo2.controller;2 3 Importjavax.servlet.ServletException;4 ImportJavax.servlet.annotation.WebServlet;5 ImportJavax.servlet.http.Cookie;6 ImportJavax.servlet.http.HttpServlet;7 Importjavax.servlet.http.HttpServletRequest;8 ImportJavax.servlet.http.HttpServletResponse;9 Importjava.io.IOException;Ten ImportJava.io.PrintWriter; One  A /** - * Created by Administrator on 2017/12/7. -  */ the@WebServlet ("/cookie") -  Public classCookieservletextendsHttpServlet { - @Override -      Public voidService (HttpServletRequest req, HttpServletResponse resp)throwsservletexception, IOException { +         //Request garbled processing -Req.setcharacterencoding ("Utf-8"); +         //Response garbled processing AResp.setcontenttype ("Text/html;charset=utf-8"); at         //Manipulating Cookies -  -         //Increase -Cookie Cookie1 =NewCookie ("Name1", "Wuji1"); - Resp.addcookie (cookie1); -Cookie Cookie2 =NewCookie ("Name2", "Wuji2"); in Resp.addcookie (cookie2); -         //Delete to        /*6): The life cycle of the cookie: (How long the shared data in the cookie can be retained) + By default: Closing the browser cookie is lost. - setmaxage (int seconds) of the cookie object: How long the cookie can be set to survive. the seconds>0: How many seconds can be survived. * seconds<0: stored in the browser process, closed browser cookie is lost. $ seconds=0: Delete cookies.Panax Notoginseng 7): Delete cookies: - Cookie object. setmaxage (0);*/ theCookie1.setmaxage (0); +         //must be Resp.addcookie (cookie) before it can take effect A Resp.addcookie (cookie1); the         //Modify +  -         //Enquiry $  $         //get Output Object -PrintWriter writer =Resp.getwriter (); -         //Output Information theWriter.println ("Hello sister!")); - Wuyi  the     } -}
Cookieservlet

1  PackageCom.day07.web.demo2.controller;2 3 Importjavax.servlet.ServletException;4 ImportJavax.servlet.annotation.WebServlet;5 ImportJavax.servlet.http.Cookie;6 ImportJavax.servlet.http.HttpServlet;7 Importjavax.servlet.http.HttpServletRequest;8 ImportJavax.servlet.http.HttpServletResponse;9 Importjava.io.IOException;Ten ImportJava.io.PrintWriter; One  A /** - * Created by Administrator on 2017/12/7. -  */ the@WebServlet ("/cookie2") -  Public classCookie2servletextendsHttpServlet { - @Override -      Public voidService (HttpServletRequest req, HttpServletResponse resp)throwsservletexception, IOException { +         //Request garbled processing -Req.setcharacterencoding ("Utf-8"); +         //Response garbled processing AResp.setcontenttype ("Text/html;charset=utf-8"); at         //Manipulating Cookies -  -         //Delete -         //Modify -  -         //Enquiry incookie[] Cookies =req.getcookies (); -          for(inti=0;i<cookies.length;i++){ toCookie cookie =Cookies[i]; +             //take key -String name =cookie.getname (); the             //Take value *String value =Cookie.getvalue (); $             //PrintPanax NotoginsengSystem.out.println (name+ "=" +value); -  theCookie.setvalue ("Zhaoming"); + Resp.addcookie (cookie); A         } the  +         //get Output Object -PrintWriter writer =Resp.getwriter (); $         //Output Information $Writer.println ("Hello sister----get cookie data!")); -  -  the     } -}
Cookie2servlet

Cookie operation of 04_web Foundation ()

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.