cookie and session principle

What is the conversation tracking technology

In JavaWeb, the first client sends a request to a server to start the session began, until the end of the customer closed the browser session. A plurality of shared data in a session request, which is session tracking technology.
We know that HTTP protocol is a stateless protocol, which means that each request is independent! State before a request can not be recorded. However, the HTTP protocol can be used to complete the Cookie session tracking! In JavaWeb, a complete session to session tracking, the underlying session Cookie technology dependent.

 

 What is a cookie

 

1) When a client first request to the server again, Cookie is created by the server, then sending a response to a client's key pair. The client will save Cookie, and will mark the source of Cookie (Cookie which server, the browser client a domain name corresponding to the contents of a cookie, cookie content contains a lot of key-value pairs).

When the client makes a request to the server again, the server will all Cookie contained in the request sent to the server so that the server can identify a client! (Expressed as the last time you visited your merchandise or last viewed).

2) Cookie is passed in by the client and server side HTTP request and response headers:
cookies: request header sent by the client to the server;
format: Cookie: a = A; b = B; c = C. I.e., away from the plurality of Cookie semicolon;
the Set-Cookie: header in response, the server sends to the client;
a Cookie-Cookie object the Set:
the Set-Cookie: A A =
the Set-Cookie: B = B
the Set-Cookie: C = C

3) two important ways:

 

Cookie [] = CS request.getCookies (); // acquisition request Cookie, the front access to all the domain names corresponding to all of the cookie to the server

response.addCookie (Cookie); // add the response Cookie objects

What is the session

1) to obtain the HttpSession object
HttpSession request.getSesssion (): If the current session has been less direct return session object if the current session does not exist session, create session and return;

2) When first using the session, the server-side to create a session, session is stored on the server side to the client while the session id. The client is taken away sessionId, and the data is stored in the session.
When the Client Access server again, the request will bring sessionId (all the cookie domain name servers are corresponding to the server, a cookie is saved in sessionId), and the server will find the corresponding session through sessionId, without having to create a new session.

 

to sum up:

cookie: cookie domain name corresponds to multiple requests for data before sharing
session: domain names corresponding cookie = sessionId, sessionId find the corresponding session, before repeated requests for data sharing

 

 

Published 159 original articles · won praise 75 · views 190 000 +

Guess you like

Origin blog.csdn.net/xuehuagongzi000/article/details/78677871