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.
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
- Top 45 Fresher Java Interview Questions - March 9, 2023
- 25 Free Practice Questions – GCP Certified Professional Cloud Architect - December 3, 2021
- 30 Free Questions – Google Cloud Certified Digital Leader Certification Exam - November 24, 2021
- 4 Types of Google Cloud Support Options for You - November 23, 2021
- APACHE STORM (2.2.0) – A Complete Guide - November 22, 2021
- Data Mining Vs Big Data – Find out the Best Differences - November 18, 2021
- Understanding MapReduce in Hadoop – Know how to get started - November 15, 2021
- What is Data Visualization? - October 22, 2021