Interview question-6 session and cookie

1.cookie

The cookie uses the client's session state, which is a small piece of information stored on the local machine of the server and sent to the server with the request.
Created by cookie cookie = new cookie, and set cookie persistence time by cookie.setMaxAge

2.Session

Session stores data in the server, and creates a memory space for each client to
store the client's data, but the client needs to carry an identification ID every time to find its own
memory space in the server . Therefore, the realization of Session is based on Cookie. Session needs to use Cookie to store the unique identification JSESSIONID of the client.
We can obtain the session object through request.getsession(), store the object through
session.setAttribute(), and get the
session information through session.getAttribute() . Life cycle: 1. When the server is shut down, 2. The session expires (30 seconds by default) 3. Manually call session.invalidate();
The scope of the session: in a session

Does the session disappear when the browser is closed?

When we close the client browser, the Session is not destroyed, it is still on the server side, but once the client closes the browser and opens a new window, the previous SessionID will no longer be accessible. Because the SessionID is stored in the browser process through a cookie, once the browser is closed, the corresponding cookie disappears. When you close the browser window, the client does not want the server to send any request, and the server accepts it. There is not anything submitted by the client, so the Session of the server is still alive there. When you reopen a window, the server will send a new SessionID for this new page. This new SessionID is obviously It is different from the previous SessionID, and there is no relationship between the two, so the client will correspond to a new session, while the original session on the server side will always exist. Wait until the timeout, the server side session will be destroyed Lost.

Guess you like

Origin blog.csdn.net/zyf_fly66/article/details/114064532