Cookie and temporarily stored timing storage

Cookie solve the problem of different data sharing requests.

1, temporary storage:

    protected  void the doGet (the HttpServletRequest Request, the HttpServletResponse Response) throws ServletException, IOException { 
        Request.setCharacterEncoding ( " UTF-. 8 " ); // encoding settings server, the default-8859-1 is the ISO 
        the response.setContentType ( " text / HTML; charset UTF-8 = " ); // tells the browser to the server encoding format 
        Cookie the cookie = new new Cookie ( " passwsord " , " 123456 " ); 
        response.addCookie (the cookie); 
    }

request:

 

response:

 

 When visiting the project of a Cookie Servlet does not exist:

 

 As can be seen in the request still exists Cookie.

But the response has no information of the Cookie:

 

 When you close the browser to open a non-existent in the engineering Servlet access again:

And in response to information requests no Cookie:

 

 

 

 This is because the first time you visit Servlet with the Cookie, Cookie is created, but because Cookie is stored in a memory to run the browser, Cookie created after the browser is closed disappear.

 2, timing storage:

    protected  void the doGet (the HttpServletRequest Request, the HttpServletResponse Response) throws ServletException, IOException { 
        Request.setCharacterEncoding ( " UTF-. 8 " ); // encoding settings server, the default-8859-1 is the ISO 
        the response.setContentType ( " text / HTML; charset UTF-8 = " ); // tells the browser to the server encoding format 
        Cookie the cookie = new new Cookie ( " passwsord " , " 123456 " ); 
        cookie.setMaxAge ( 7 * 24- * 3600 ); // seven days 
        response.addCookie (the cookie);
    }

First visit Servlet with Cookie's:

 

 

 

 Requests and responses are Cookie.

Servlet Cookie does not have access under the same project:

 

 

 

 There request Cookie, no response Cookie.

Close your browser without re-visit Servlet Cookie's:

 

 

 

 Request with a Cookie, no response. This is because the Cookie is set to its expiration date, you can be found in the life of the computer's hard drive, even if you close the browser, within the validity period Cookie remains.

Guess you like

Origin www.cnblogs.com/zhai1997/p/11530721.html