Example & Tutorial understanding programming in easy ways.

What are sessions in Servlets?

As we know that the http protocol is the stateless protocol, it do`t save the clints information. 
The web container which we use then create the session id for each user. by which the user get identified.

We can get the session object and attributes. The HttpServletRequest interface provide the the two methods for the object of HttpSesion.
public HttpSession getSession(); This method returns the current session associated with the request and crete the sesion if the request do`t have the one.
Public HttpSession setSession(boolean create); It return the current http session associated with the current request and if the request does`t have session then crete one for that and return that session.

There are the list of the methods mostly used in the HttpSession maintence, they are as follows;

1. public String getId():Returns a string containing the unique identifier value that can be used to identifey the session during the life cycle of the client request.
2. public long getCreationTime():Returns the time when this session was created, measured in milliseconds since midnight January 1, 1970 GMT.
3. public 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.
4. public void invalidate():Invalidates this session then unbinds any objects bound to it and make free from session.

Read More →