0% found this document useful (0 votes)
6 views

Session Tracking Techniques

Session tracking in servlets is essential for maintaining user state in the stateless HTTP protocol, utilizing techniques like cookies, hidden form fields, URL rewriting, and HttpSession. Each method has its advantages and disadvantages, such as cookies being simple but dependent on browser settings, while URL rewriting is browser-independent but limited to links. HttpSession provides a robust way to manage user sessions through unique session IDs, allowing for object binding and session information manipulation.

Uploaded by

vishnu060405
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Session Tracking Techniques

Session tracking in servlets is essential for maintaining user state in the stateless HTTP protocol, utilizing techniques like cookies, hidden form fields, URL rewriting, and HttpSession. Each method has its advantages and disadvantages, such as cookies being simple but dependent on browser settings, while URL rewriting is browser-independent but limited to links. HttpSession provides a robust way to manage user sessions through unique session IDs, allowing for object binding and session information manipulation.

Uploaded by

vishnu060405
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 19

SESSION TRACKING IN SERVLET

Session Tracking…
• Session simply means a particular interval of time.
• Session Tracking is a way to maintain state (data) of
an user. It is also known as session management in
servlet.
• Http protocol is a stateless so we need to maintain
state using session tracking techniques. Each time
user requests to the server, server treats the
request as the new request. So we need to maintain
the state of an user to recognize to particular user.
Session Tracking…
• HTTP is stateless that means each request is
considered as the new request. It is shown in
the figure given below:
Why use Session Tracking?

• To recognize the user It is used to recognize


the particular user.
Session Tracking Techniques
• There are four techniques used in Session
tracking:
1. Cookies
2. Hidden Form Field
3. URL Rewriting
4. HttpSession
Cookies
• A cookie is a small piece of information that is
persisted between the multiple client
requests.
• A cookie has a name, a single value, and
optional attributes such as a comment, path
and domain qualifiers, a maximum age, and a
version number.
How Cookie works
• By default, each request is considered as a
new request. In cookies technique, we add
cookie with response from the servlet.
• So cookie is stored in the cache of the
browser. After that if request is sent by the
user, cookie is added with request by default.
• Thus, we recognize the user as the old user
Cookie…
• Advantage of Cookies
• Simplest technique of maintaining the state.
• Cookies are maintained at client side.
• Disadvantage of Cookies
• It will not work if cookie is disabled from the
browser.
• Only textual information can be set in Cookie
object.
Cookie class

• Constructor:
• Cookie() : constructs a cookie.
• Cookie(String name, String value): constructs a cookie
with a specified name and value.
• Methods
• public void setMaxAge(int expiry):Sets the maximum
age of the cookie in seconds.
• public String getName(): Returns the name of the
cookie. The name cannot be changed after creation.
• public String getValue(): Returns the value of the cookie.
• public void setName(String name): changes the name of
the cookie.
• public void setValue(String value): changes the value of
the cookie.
Methods…
• public void addCookie(Cookie ck):method of
HttpServletResponse interface is used to add
cookie in response object.
• public Cookie[] getCookies():method of
HttpServletRequest interface is used to return
all the cookies from the browser.
URL rewriting
• In URL rewriting, we append a token or
identifier to the URL of the next Servlet or the
next resource. We can send parameter
name/value pairs using the following format:
• url?name1=value1&name2=value2
• A name and a value is separated using an
equal = sign, a parameter name/value pair is
separated from another parameter using the
ampersand(&).
URL Rewriting…
Advantage of URL Rewriting
• It will always work whether cookie is disabled
or not (browser independent).
• Extra form submission is not required on each
pages.
Disadvantage of URL Rewriting
• It will work only with links.
• It can send Only textual information.
HttpSession

• In such case, container creates a session id for


each user.The container uses this id to identify
the particular user.An object of HttpSession
can be used to perform two tasks:
• bind objects
• view and manipulate information about a
session, such as the session identifier, creation
time, and last accessed time.
HttpSession…
The HttpServletRequest interface provides two
methods to get the object of HttpSession:
• public HttpSession getSession():Returns the
current session associated with this request, or if
the request does not have a session, creates one.
• public HttpSession getSession(boolean
create):Returns the current HttpSession
associated with this request or, if there is no
current session and create is true, returns a new
session.
Hidden Form field
• In case of Hidden Form Field a hidden (invisible)
textfield is used for maintaining the state of an
user.
• In such case, we store the information in the
hidden field and get it from another servlet. This
approach is better if we have to submit form in all
the pages and we don't want to depend on the
browser.
• Let's see the code to store value in hidden field.
• <input type="hidden" name="uname" value=“Anu
rag University">
Real application of hidden form field

• It is widely used in comment form of a


website. In such case, we store page id or
page name in the hidden field so that each
page can be uniquely identified.
Hidden form field
• Advantage of Hidden Form Field
• It will always work whether cookie is disabled
or not.
• Disadvantage of Hidden Form Field:
• It is maintained at server side.
• Extra form submission is required on each
pages.
• Only textual information can be used.
THANK YOU

You might also like