Java Web----Cookies

Source: Internet
Author: User
Tags java web

1 we know that the HTTP protocol is a stateless protocol, which means that each request is independent! The status of the previous request cannot be logged. But the HTTP protocol can use cookies to complete session tracking!

In Javaweb, sessions are used to complete session tracking, which relies on cookie technology.

2 cookies


A cookie is a key-value pair that is created by the server and then sent to the client by a response. The client saves the cookie and marks the source of the cookie (which server's cookie). When a client makes a request to the server, it sends all of this server cookie to the server in the request, so the server can identify the client!

3 Cookie Example 1

Client Access Aservlet,aservlet adds a cookie to the response, and the browser automatically saves the cookie. The client then accesses Bservlet, and the browser automatically prints the cookie in the request with the Cookie,bservlet request.


Package Com.cug.cookie;import Java.io.ioexception;import Java.util.uuid;import javax.servlet.ServletException; Import Javax.servlet.http.cookie;import Javax.servlet.http.httpservlet;import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.httpservletresponse;public class AServlet extends httpservlet{@Overrideprotected void Doget (HttpServletRequest req, HttpServletResponse resp) throws Servletexception, IOException {resp.setcontenttype ("text/html;charset=utf-8"); String id = uuid.randomuuid (). toString (); Cookie ck1 = new Cookie ("id", id); Resp.addcookie (CK1); Resp.getwriter (). Print ("The cookie has been set! ");}}

Package Com.cug.cookie;import Java.io.ioexception;import Javax.servlet.servletexception;import Javax.servlet.http.cookie;import Javax.servlet.http.httpservlet;import javax.servlet.http.HttpServletRequest; Import Javax.servlet.http.httpservletresponse;public class Bservlet extends httpservlet{@Overrideprotected void doget (HttpServletRequest req, HttpServletResponse resp) throws Servletexception, IOException {resp.setcontenttype ("text/ Html;charset=utf-8 "); cookie[] cookies = req.getcookies (); for (Cookie c:cookies) {if (C.getname (). Equals ("id")) {Resp.getwriter (). Print ( C.getvalue ());}}}

4 case: Show last access time

Package Com.cug.cookie;import Java.io.ioexception;import Java.util.date;import javax.servlet.ServletException; Import Javax.servlet.http.cookie;import Javax.servlet.http.httpservlet;import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.httpservletresponse;public class CServlet extends httpservlet{@Overrideprotected void Doget (HttpServletRequest req, HttpServletResponse resp) throws Servletexception, IOException {resp.setcontenttype ("text/html;charset=utf-8"); Cookie CK = new Cookie ("Lasttime", (New Date ()). ToString ()); Resp.addcookie (CK); String s = "You are the first to visit this site!" "; cookie[] cookies = req.getcookies (), if (cookies! = null) {for (cookie c:cookies) {if (C.getname (). Equals ("Lasttime")) {s = " The last time you visited was: "+c.getvalue ();}}} Resp.getwriter (). print (s);}}

5 Chinese cookie name needs to be encoded

Package Com.cug.cookie;import Java.io.ioexception;import Java.net.urldecoder;import java.net.urlencoder;import Javax.servlet.servletexception;import Javax.servlet.http.cookie;import Javax.servlet.http.httpservlet;import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.httpservletresponse;public class DServlet extends httpservlet{@Overrideprotected void Doget (HttpServletRequest req, HttpServletResponse resp) throws Servletexception, IOException {resp.setcontenttype ("text/html;charset=utf-8"); String name = Urlencoder.encode ("Zhu", "Utf-8"); String value = Urlencoder.encode ("Zhu", "Utf-8"); Cookie CK = new Cookie (name,value); Resp.addcookie (CK); cookie[] cookies = req.getcookies (); for (Cookie c:cookies) {if (C.getname (). Equals (name)) {String name1 = Urldecoder.decode (C.getname (), "utf-8"); String value1 = Urldecoder.decode (C.getvalue (), "Utf-8"), Resp.getwriter (). Print (name1 + ":" + value1);}}}


Java Web----Cookies

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.