Cs378 Slide07 Session Tracking
Cs378 Slide07 Session Tracking
CS378 – SLIDE – 07
SESSION TRACKING
1
To Cover
Overview of Session Tracking
What is a session?
2
OVERVIEW OF SESSION
TRACKING
3
What is a Session?
A session is:
4
What Session Tracking?
Session tracking is a mechanism used by the Web
container to store session information for a
particular user
5
Why is Session Tracking
Required?
HTTP (Hypertext Transfer Protocol) is the set
of rules for transferring files such as text,
images, sound, video and other multimedia
files over the web
6
Why is Session Tracking
Required?
HTTP is a “stateless” protocol, which means that
7
Why is Session Tracking
Required?
HTTP is a “stateless” protocol, which means that
Session tracking is
important for tracking
Session tracking is
conversions in online
used to recognize
shopping, mailing
the particular user
applications, and E-
Commerce
applications 8
SESSION TRACKING
TECHNIQUES
9
Session Tracking Techniques
There are four session tracking techniques
Cookies
URL Rewriting
HttpSession
10
Cookies
A cookie is a key value pair of information, sent by the server
to the browser and then browser sends back this identifier to
the server for subsequent request(s) there on
Disadvantage
Advantage cookies can be deleted /
One of the simplest disables by client
technique for session
tracking
11
Cookies
Steps of sending cookies to the client
12
Hidden Form Field
The hidden form field is used to insert the information in the
webpages and this information is sent to the server
Disadvantage
(1) Lots of tedious processing
(2) All pages must be the
Advantage
result of form submissions
Works even if cookies are
disabled or unsupported
13
URL Rewriting
We can append some extra data at the end of each URL that
identifies the session, and the server can associate that
session identifier with data it has stored about that session
Example:
http://www.rcyci.com/file.htm;sessionid = 12345
Disadvantage
(1) Lots of tedious processing
(2) All pages must be
dynamically generated
Advantage
because you need to add
Works even if cookies are
userdata to url submissions
disabled or unsupported
14
HttpSession API
15
HttpSession API
Session tracking in servlet is very simple
and it involves following steps
Accessing the session object associated with the
current request
If the user
If you want to
already has a If no session
know if this is
session the exists a new
a new session,
existing one is created
call isNew()
session is and returned
method
returned
17
Accessing the Session Object
To disable creation of new sessions, pass false to the
getSession() method.
HttpSession session = request.getSession(false);
If no current
session exists,
you will get
back a null
object
18
Looking up Information
Associated with a Session
HttpSession objects live on the server; they
don’t go back and forth over the network
19
Behind the Scene
20
Looking up Information
Associated with a Session
The return value is null if there is no such attribute, so
you need to check for null before calling methods on
objects associated with sessions
21
Storing Information in a Session
22
Discarding Session Data
23
HttpSession Methods
24
HttpSession Methods
25
CLASS WORK
26
Class Work
• What is session tracking?
27
Class Work
• Why do we need session tracking?
28
Class Work
• HTTP is a stateless protocol, what does it
mean?
It means each time a client requests a Web page,
the client establishes a new connection with the
Web server and the server does not retain track of
prior requests
29
Class Work
• How many session tracking techniques
are there? Mention them.
There are four session tracking techniques:
30
Class Work
• Write an HTML code to create a hidden
field that can be used for session tracking
31
Class Work
• Mention any disadvantage of using
cookies for session tracking
32
Class Work
HttpSession s = request.getSession();
HttpSession s = request.getSession(fales);
33
Class Work
• Write a command to discard the session
and delete all its attributes?
session.invalidate();
34
Class Work
• By default, how long does a session last?
35
SESSION TRACKING
EXAMPLES
36
Printing Session Information
Example 1
37
Printing Session Information
Example 1
38
Storing / Retrieving Session
Object - Example 2
• Write an example to demonstrate storing
and retrieving of an object from session
39
Storing / Retrieving Session
Object - Example 2
40
Storing / Retrieving Session
Object - Example 2
41
Invalidating Session
Example 3
42
Invalidating Session
Example 3
43
Invalidating Session
Example 3
44
THANK
YOU
45
? 46