Module 3 Part 2-1
Module 3 Part 2-1
● The HTTP protocol is stateless, which implies that each request is treated
as a new one. As you can see in the image below.
Session Tracking employs Four Different techniques
● Cookies
● Hidden Form Field
● URL Rewriting
● HttpSession
Cookies
● Cookies are little pieces of data delivered by the web server in the
response header and kept by the browser.
● Each web client can be assigned a unique session ID by a web
server.
● Cookies are used to keep the session going.
● Cookies can be turned off by the client.
How cookies 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.
Types of Cookie
1. Non-persistent cookie
2. Persistent cookie
Non-persistent cookie
It is valid for single session only. It is removed each time when user closes the browser.
Persistent cookie
It is valid for multiple session . It is not removed each time when user closes the browser. It
is removed only if user logout or signout.
Simple example of Servlet Cookies
Hidden Form Field
● The information is inserted into the web pages via the hidden form field,
which is then transferred to the server.
● These fields are hidden from the user’s view.
● Illustration:
Session.setAttribute("username", "password");
Methods provided by the HttpSession object:
Method Description
public Object getAttribute(String This method returns the object in this session bound with the supplied name, or null if
name) no object is bound with the name.
public Enumeration This function returns an Enumeration of String objects with the names of all the items
getAttributeNames() associated with this session.
This method returns the milliseconds since midnight January 1, 1970 GMT, when this
public long getCreationTime()
session was created.
Method Description
public String getId() This function returns a string that contains the session’s unique identification.
public long This function returns the session’s most recent accessible time in milliseconds since
getLastAccessedTime() midnight on January 1, 1970 GMT.
public int The maximum time interval (seconds) for which the servlet container will keep the
getMaxInactiveInterval() session open between client requests is returned by this function.
public void invalidate() This function unbinds any objects connected to this session and invalidates it.
public boolean isNew() If the client is unaware of the session or decides not to join it, this function returns true.
Methods provided by the HttpSession object cont..
Method Description
public void removeAttribute(String The object bound with the supplied name is removed from this session using
name) this method.
public void setMaxInactiveInterval(int This function defines the interval between client requests before the servlet
interval) container invalidates this session in seconds.
Example