Managing Session in Servlets
We all know that HTTP is a stateless protocol.
All requests and responses are independent. But sometimes you need to keep
track of client's activity across multiple requests. For eg. When a User logs
into your website, not matter on which web page he visits after logging in, his
credentials will be with the server, until he logs out. So this is managed by
creating a session.
Session Management is a mechanism used by
the Web container to store session information for a particular
user. There are four different techniques used by Servlet application for
session management. They are as follows:
1.
Cookies
2.
Hidden form field
3.
URL Rewriting
4.
HttpSession
What is HttpSession?
HttpSession object is used to store entire session
with a specific client. We can store, retrieve and remove attribute from HttpSession object.
Any servlet can have access to HttpSession object throughout
the getSession() method of the HttpServletRequest object.
Using URL Rewriting for Session
Management
If the client has disabled cookies in the browser then session
management using cookie wont work. In that case URL Rewriting can
be used as a backup. URL rewriting will always work.
In URL rewriting, a token(parameter) is added at the end of the
URL. The token consist of name/value pair seperated by an
equal(=)
sign.