I read that isNew () returns true if the client has not yet responded with a session identifier. But what does that mean?
Note that the server is processing the request. There are two scenarios for processing a session.
In the new session scenario, a new session is created for the user / client by the server. (The client may not have provided a session identifier in the request, or it could have provided a session identifier that the server considers invalid.) The servlet application code decides that the session is necessary (for example, because it has some information that it wants to store there) and try to extract it using the "create if not present" flag. The servlet infrastructure understands that there is no current session, a new one is created with a new session identifier, and it is stored in the session store. Upon completion of the request, the session identifier is returned to the client; for example, as a cookie, or as a URL with an attached session identifier.
In an existing session scenario, the client included the session identifier in the request; for example, as a session cookie or as a session identifier in the request URL. The servlet infrastructure recognizes this identifier, looks at it in the session store and, if necessary, recreates an HttpSession object containing the session state retrieved from the session store. When the servlet application code tries to access the session, it receives this HttpSession object, not a new one. Session state can then be used and updated by the servlet as it is processed.
In the first scenario, calling isNew() on the session object will return true because it is a new session.
In the second scenario, calling isNew() on the session object will return false because it is NOT a new session.
Like calling isNew () on a session object, check if the session is new or already in use?
The servlet infrastructure knows which of the two scenarios occurred because it created a session or session lookup. The most obvious implementation of isNew() is to include the private boolean field in the HttpSession object and return the value of this field as a result of isNew() . The field will be initialized by the servlet infrastructure according to how it received the session object.
Stephen c
source share