Session Tracking in Servlets Unit 2
Session Tracking in Servlets Unit 2
->Http protocol is a stateless,so we need to maintain state using session tracking techniques.
->Each time user requests t o the server,server treats the requests as the new requests.
->so we need to maintain the state of an user to recognize the particular user.
->Http is stateless that means each request is considered as the new request.All req& response are
independent.
3)Second request(new)
Client
1)Request(new)
server
2)response
Client1
server
Request id=123
Web container
Session
Client2 id=123
servlet
request id=124 Session
id=124
Cookies in Servlet:
A cookie is a small piece of information that is persisted b/w the multiple client request.
It has a name,single value attributes such as a comment,path& domain qualifier,a max age and
version no:
3.request+cookie
User
1.request server
2.response+cookies
By default each cookie request is considered as a new request .in cookie technique we add cookie
with response from the servlet.so cookies is stored in a cache of a 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
1)Non-Persistent Cookie:It is Valid for single session only.It is removed each time when user closes
the browser.
2)Persistent cookie: It is valid for multiple session.it is not removed each time when the user closes
browser.it is removed only if usr logout of the session.
Advantages:
Disadvantage:
Cookie class:
Cookie class provides the method and functionality for session management using cookie.
Constructor Description
->Cookie(string name,string value) constructs a cookie with specified name & value
Method Description
Public void setMaxAge(int expiry) - Sets the max age of the cooki in second s
Public string getName() - Returns the name of the cookie.The name cannot be changed after
creation.
For(int i=0;i<ck.length;i++)
{
HTTP responses
When a web server has sent a web page to a browser, the connection is shut
down, and the server forgets everything about the user.
When a browser requests a web page from a server, cookies belonging to the
page are added to the request. This way the server gets the necessary data
to "remember" information about users.
You can also add an expiry date (in UTC time). By default, the cookie is
deleted when the browser is closed:
With a path parameter, you can tell the browser what path the cookie
belongs to. By default, the cookie belongs to the current page.
let x = document.cookie;
document.cookie will return all cookies in one string much like: cookie1=value;
cookie2=value; cookie3=value;
You don't have to specify a cookie value when you delete a cookie.
You should define the cookie path to ensure that you delete the right cookie.
Some browsers will not let you delete a cookie if you don't specify the path.
If you set a new cookie, older cookies are not overwritten. The new cookie is
added to document.cookie, so if you read document.cookie again you will get
something like:
Display All Cookies Create Cookie 1 Create Cookie 2 Delete Cookie 1 Delete
Cookie 2
If you want to find the value of one specified cookie, you must write a
JavaScript function that searches for the cookie value in the cookie string.
The first time a visitor arrives to the web page, he/she will be asked to fill in
his/her name. The name is then stored in a cookie.
The next time the visitor arrives at the same page, he/she will get a welcome
message.
Example
function setCookie(cname, cvalue, exdays) {
const d = new Date();
d.setTime(d.getTime() + (exdays*24*60*60*1000));
let expires = "expires="+ d.toUTCString();
document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
}
Example explained:
The parameters of the function above are the name of the cookie (cname),
the value of the cookie (cvalue), and the number of days until the cookie
should expire (exdays).
The function sets a cookie by adding together the cookiename, the cookie
value, and the expires string.
Example
function getCookie(cname) {
let name = cname + "=";
let decodedCookie = decodeURIComponent(document.cookie);
let ca = decodedCookie.split(';');
for(let i = 0; i <ca.length; i++) {
let c = ca[i];
while (c.charAt(0) == ' ') {
c = c.substring(1);
}
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return "";
}
Function explained:
Create a variable (name) with the text to search for (cname + "=").
Decode the cookie string, to handle cookies with special characters, e.g. '$'
Split document.cookie on semicolons into an array called ca (ca =
decodedCookie.split(';')).
Loop through the ca array (i = 0; i < ca.length; i++), and read out each
value c = ca[i]).
If the cookie is found (c.indexOf(name) == 0), return the value of the cookie
(c.substring(name.length, c.length).