Session Management

Knowingly or unknowingly ‘sessions’ are part of our daily digital life and this post seeks to explain the concepts related to ‘session management’ in Java. ‘Session Management’ is an interesting topic and it is also one of the topics for the ‘Web component developer 6’ exam.

Introduction:

This post assumes understanding of servlets, web container and other basic terms. To put it briefly, a ‘servlet’ is a small program that runs inside a web server. A ‘servlet container’ aids in the management of loading and running servlets.

HTTP:

Unlike other protocols like FTP or TCP, HTTP is stateless protocol. A ‘stateless protocol’ is one in which the client and server do not remember each other and there is no persistent connection. The Web container thinks of each request by the client as a new one. This prevents conversations between the client and server which is an absolute necessity in today’s online environment.

‘Sessions’ enable conversations between the client and server by means of ‘cookies’ or URL rewriting.

Sessions:

‘Sessions’ always makes one conjure thoughts related to shopping baskets and online shopping and rightly so. ‘Session management’ deals with the servlet container creating a session with a client and interacting with the user over multiple requests. Once the interaction is over, the session is invalidated.

In order to work with sessions, the ‘HttpSession’ interface must be imported. It is present in the javax.servlet.http package.

Let us first see how session management works with ‘cookies’. The client initiates a conversation with the server and the container responds with a unique session id that will be used for future conversations.session tracking

This is illustrated in the figure above. During each request, the client and server use the assigned session id which will enable it to have a smooth interaction. Once the interaction is completed, the session is invalidated.

Methods in Http Session interface:

These are some of the important methods relating to HttpSession interface. The most important method which deals with creating sessions is present in ‘HttpServletRequest’ interface which is:

HttpSession getSession();

This method returns an existing session if one exists or creates a new one if it is not present.

Hence this code,

HttpSession session=request.getSession();

will create a new session if it is not present where ‘request’ is

‘HttpServletRequest’ interface.

This code:

HttpSession session= request.getSession(boolean var);

returns a pre-existing session. However, if the session does not exist and the value of the boolean variable ‘var’ is ‘false’, it returns ‘null’.

Again, if the session does not exist and if the value of the boolean variable ‘var’ is ‘true’, it creates a new session.

‘request’ is again of type ‘HttpServletRequest’ interface.

The other methods are:

1. void invalidate();

This invalidates the session.

2. boolean isNew();

This method is used to determine whether the client has chosen to join a session created by the server. This method returns ‘true’ if the client chooses not to join a conversation.

3. Object getAttribute(String);

4. void setAttribute(String, Object);

5. void removeAttribute(String);

The getAttribute(), setAttribute() and removeAttribute() method are used to retrieve attributes, set attributes and remove attributes in session scope.

This post dealt with handling ‘session management’ and ‘cookies’. The next post will deal with session management and URL rewriting.

Image Credits: Google

About Aditi Malhotra

Aditi Malhotra is the Content Marketing Manager at Whizlabs. Having a Master in Journalism and Mass Communication, she helps businesses stop playing around with Content Marketing and start seeing tangible ROI. A writer by day and a reader by night, she is a fine blend of both reality and fantasy. Apart from her professional commitments, she is also endearing to publish a book authored by her very soon.

Leave a Comment

Your email address will not be published. Required fields are marked *


Scroll to Top