What do these calls actually mean in terms of session?
System.out.println("print1: "+request.getSession().getId()); System.out.println("print2: "+request.getSession(false));
OUTPUT
print1: D94146A347D95563186EB7525726336B print2: org.apache.catalina.session.StandardSessionFacade@d52411
request. getSession(false) will return current session if current session exists. If not, it will not create a new session. Follow this answer to receive notifications.
The default behavior of this method is to return getSession() on the wrapped request object. Returns the current HttpSession associated with this request or, if there is no current session and create is true, returns a new session.
getId() Returns a string containing the unique identifier assigned to this session. long. getLastAccessedTime() Returns the last time the client sent a request associated with this session, as the number of milliseconds since midnight January 1, 1970 GMT, and marked by the time the container received the request.
- getSession(true) will check whether a session already exists for the user. If yes, it will return that session object else it will create a new session object and return it.
HttpSession session = request.getSession();
Inside the service method we ask for the session and every thing gets automatically, like the creation of the HttpSession object. There is no need to generate the unique session id. There is no need to make a new Cookie object. Everything happens automatically behind the scenes.
As soon as call the method getSession()
of the request object a new object of the session gets created by the container and a unique session id generated to maintain the session. This session id is transmitted back to the response object so that whenever the client makes any request then it should also attach the session id with the requsest object so that the container can identify the session.
request.getSession(false)
: This method will check whether Session already existed for the request or not. If it existed then it will return the already existed Session. If Session is not already existed for this request then this method will return NULL, that means this method says that the request does not have a Session previously.
In short-
request.getSession().getId()
- returns a string containing the unique identifier assigned to this session. The identifier is assigned by the servlet container and is implementation dependent.
request.getSession(false)
- return session object or null if there's no current session.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With