Using cookies and sessions

Nuha Noor
Using Cookies and Sessions
By
Sana Mateen
Cookie vs session
cookie
• A cookie is a small piece of text stored
on a user's computer by their browser.
Common uses for cookies are
authentication, storing of site
preferences, shopping cart items, and
server session identification.
• Each time the users' web browser
interacts with a web server it will pass
the cookie information to the web
server. Only the cookies stored by the
browser that relate to the domain in
the requested URL will be sent to the
server. This means that cookies that
relate to www.example.com will not be
sent to www.exampledomain.com.
• In essence, a cookie is a great way of
linking one page to the next for a user's
interaction with a web site or web
application.
session
• A session can be defined as a server-
side storage of information that is
desired to persist throughout the
user's interaction with the web site or
web application.
• Instead of storing large and constantly
changing information via cookies in
the user's browser, only a unique
identifier is stored on the client side
(called a "session id"). This session id
is passed to the web server every time
the browser makes an HTTP request
(ie a page link or AJAX request). The
web application pairs this session id
with it's internal database and
retrieves the stored variables for use
by the requested page.
• 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.
• For Example:
• Flipkart uses session to maintain
thedetails of various products
selected by its customers, which
is called cart.
• Session Tracking is a way to
maintain state (data) of an user. It
is also known as session
management in servlet.
Cookies in servlets
• A cookie is a small piece of information that is persisted between the
multiple client requests.
• 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.
• Gmail uses cookie technique for login. If you disable the cookie, gmail
won't work.
Using cookies and sessions
Using cookies and sessions
Using cookies and sessions
Using cookies and sessions
Using cookies and sessions
Sessions
• Servlet API provides Session management through HttpSession interface.
We can get session from HttpServletRequest object using following
methods. HttpSession allows us to set objects as attributes that can be
retrieved in future requests.
• HttpSession getSession() – This method always returns a HttpSession
object. It returns the session object attached with the request, if the
request has no session attached, then it creates a new session and return
it.
• HttpSession getSession(boolean flag) – This method returns HttpSession
object if request has session else it returns null.
• Some of the important methods of HttpSession are:
• String getId() – Returns a string containing the unique identifier assigned to this
session.
• Object getAttribute(String name) – Returns the object bound with the specified
name in this session, or null if no object is bound under the name. Some other
methods to work with Session attributes
are getAttributeNames(), removeAttribute(String name) and setAttribute(String
name, Object value).
• long getCreationTime() – Returns the time when this session was created, measured
in milliseconds since midnight January 1, 1970 GMT. We can get last accessed time
with getLastAccessedTime() method.
• setMaxInactiveInterval(int interval) – Specifies the time, in seconds, between client
requests before the servlet container will invalidate this session. We can get session
timeout value from getMaxInactiveInterval() method.
• ServletContext getServletContext() – Returns ServletContext object for the
application.
• boolean isNew() – Returns true if the client does not yet know about the session or if
the client chooses not to join the session.
• void invalidate() – Invalidates this session then unbinds any objects bound to it.
Using cookies and sessions
home.html
index.html
op.html
res.html
Using cookies and sessions
Using cookies and sessions
Using cookies and sessions
Using cookies and sessions
Using cookies and sessions
Using cookies and sessions
Using cookies and sessions
1 de 21

Recomendados

Ajax ppt por
Ajax pptAjax ppt
Ajax pptOECLIB Odisha Electronics Control Library
9.1K vistas17 diapositivas
Asynchronous JavaScript & XML (AJAX) por
Asynchronous JavaScript & XML (AJAX)Asynchronous JavaScript & XML (AJAX)
Asynchronous JavaScript & XML (AJAX)Adnan Sohail
4.3K vistas31 diapositivas
Ajax Ppt 1 por
Ajax Ppt 1Ajax Ppt 1
Ajax Ppt 1JayaPrakash.m
8.9K vistas27 diapositivas
Ajax por
AjaxAjax
AjaxTech_MX
7.5K vistas30 diapositivas
Java Beans por
Java BeansJava Beans
Java BeansAnkit Desai
8.3K vistas60 diapositivas
Ajax.ppt por
Ajax.pptAjax.ppt
Ajax.pptMAGNA COLLEGE OF ENGINEERING
9.1K vistas13 diapositivas

Más contenido relacionado

La actualidad más candente

Introduction to Angularjs por
Introduction to AngularjsIntroduction to Angularjs
Introduction to AngularjsManish Shekhawat
67.3K vistas19 diapositivas
Java Collections Framework por
Java Collections FrameworkJava Collections Framework
Java Collections FrameworkSony India Software Center
4.5K vistas36 diapositivas
Servlet.ppt por
Servlet.pptServlet.ppt
Servlet.pptVMahesh5
233 vistas15 diapositivas
Introduction to ajax por
Introduction to ajaxIntroduction to ajax
Introduction to ajaxNir Elbaz
61.6K vistas27 diapositivas
jstl ( jsp standard tag library ) por
jstl ( jsp standard tag library )jstl ( jsp standard tag library )
jstl ( jsp standard tag library )Adarsh Patel
2.7K vistas22 diapositivas
Ajax Ppt por
Ajax PptAjax Ppt
Ajax PptHema Prasanth
8.7K vistas27 diapositivas

La actualidad más candente(20)

Servlet.ppt por VMahesh5
Servlet.pptServlet.ppt
Servlet.ppt
VMahesh5233 vistas
Introduction to ajax por Nir Elbaz
Introduction to ajaxIntroduction to ajax
Introduction to ajax
Nir Elbaz61.6K vistas
jstl ( jsp standard tag library ) por Adarsh Patel
jstl ( jsp standard tag library )jstl ( jsp standard tag library )
jstl ( jsp standard tag library )
Adarsh Patel2.7K vistas
Event In JavaScript por ShahDhruv21
Event In JavaScriptEvent In JavaScript
Event In JavaScript
ShahDhruv213.2K vistas
Introduction to HTML5 por Gil Fink
Introduction to HTML5Introduction to HTML5
Introduction to HTML5
Gil Fink13.7K vistas
Ajax ppt - 32 slides por Smithss25
Ajax ppt - 32 slidesAjax ppt - 32 slides
Ajax ppt - 32 slides
Smithss2537.7K vistas
ASP.NET MVC. por Ni
ASP.NET MVC.ASP.NET MVC.
ASP.NET MVC.
Ni 1.1K vistas
Lecture 3: Servlets - Session Management por Fahad Golra
Lecture 3:  Servlets - Session ManagementLecture 3:  Servlets - Session Management
Lecture 3: Servlets - Session Management
Fahad Golra2.8K vistas

Destacado

Reading init param por
Reading init paramReading init param
Reading init paramNuha Noor
508 vistas15 diapositivas
Jsp elements por
Jsp elementsJsp elements
Jsp elementsNuha Noor
1.3K vistas19 diapositivas
Dom parser por
Dom parserDom parser
Dom parsersana mateen
1.3K vistas19 diapositivas
Xml dom por
Xml domXml dom
Xml domsana mateen
1.1K vistas9 diapositivas
Http request and http response por
Http request and http responseHttp request and http response
Http request and http responseNuha Noor
3.2K vistas13 diapositivas
Xml schema por
Xml schemaXml schema
Xml schemasana mateen
591 vistas9 diapositivas

Destacado(13)

Reading init param por Nuha Noor
Reading init paramReading init param
Reading init param
Nuha Noor508 vistas
Jsp elements por Nuha Noor
Jsp elementsJsp elements
Jsp elements
Nuha Noor1.3K vistas
Http request and http response por Nuha Noor
Http request and http responseHttp request and http response
Http request and http response
Nuha Noor3.2K vistas
Quiz app(j tabbed pane,jdialog,container,actionevent,jradiobutton,buttongroup... por Nuha Noor
Quiz app(j tabbed pane,jdialog,container,actionevent,jradiobutton,buttongroup...Quiz app(j tabbed pane,jdialog,container,actionevent,jradiobutton,buttongroup...
Quiz app(j tabbed pane,jdialog,container,actionevent,jradiobutton,buttongroup...
Nuha Noor810 vistas
Events1 por Nuha Noor
Events1Events1
Events1
Nuha Noor888 vistas
Jdbc in servlets por Nuha Noor
Jdbc in servletsJdbc in servlets
Jdbc in servlets
Nuha Noor830 vistas
Understanding layout managers por Nuha Noor
Understanding layout managersUnderstanding layout managers
Understanding layout managers
Nuha Noor728 vistas

Similar a Using cookies and sessions

Enterprise java unit-2_chapter-3 por
Enterprise  java unit-2_chapter-3Enterprise  java unit-2_chapter-3
Enterprise java unit-2_chapter-3sandeep54552
561 vistas21 diapositivas
Ecom2 por
Ecom2Ecom2
Ecom2Santosh Pandey
1.1K vistas34 diapositivas
Enterprise java unit-2_chapter-2 por
Enterprise  java unit-2_chapter-2Enterprise  java unit-2_chapter-2
Enterprise java unit-2_chapter-2sandeep54552
138 vistas18 diapositivas
Session tracking in servlets por
Session tracking in servletsSession tracking in servlets
Session tracking in servletsvishal choudhary
388 vistas19 diapositivas
Servlet sessions por
Servlet sessionsServlet sessions
Servlet sessionsvantinhkhuc
3.9K vistas20 diapositivas
C# cookieless session id and application state por
C# cookieless session id and application stateC# cookieless session id and application state
C# cookieless session id and application stateMalav Patel
399 vistas16 diapositivas

Similar a Using cookies and sessions(20)

Enterprise java unit-2_chapter-3 por sandeep54552
Enterprise  java unit-2_chapter-3Enterprise  java unit-2_chapter-3
Enterprise java unit-2_chapter-3
sandeep54552561 vistas
Enterprise java unit-2_chapter-2 por sandeep54552
Enterprise  java unit-2_chapter-2Enterprise  java unit-2_chapter-2
Enterprise java unit-2_chapter-2
sandeep54552138 vistas
Servlet sessions por vantinhkhuc
Servlet sessionsServlet sessions
Servlet sessions
vantinhkhuc3.9K vistas
C# cookieless session id and application state por Malav Patel
C# cookieless session id and application stateC# cookieless session id and application state
C# cookieless session id and application state
Malav Patel399 vistas
Session,cookies por rkmourya511
Session,cookiesSession,cookies
Session,cookies
rkmourya511369 vistas
Session And Cookies In Servlets - Java por JainamParikh3
Session And Cookies In Servlets - JavaSession And Cookies In Servlets - Java
Session And Cookies In Servlets - Java
JainamParikh3114 vistas
State management por Lalit Kale
State managementState management
State management
Lalit Kale1.3K vistas
Cookies authentication por Rsilwal123
Cookies authenticationCookies authentication
Cookies authentication
Rsilwal123166 vistas
Module-5_WTA_Managing State & jQuery por SIVAKUMAR V
Module-5_WTA_Managing State & jQueryModule-5_WTA_Managing State & jQuery
Module-5_WTA_Managing State & jQuery
SIVAKUMAR V31 vistas

Último

Monthly Information Session for MV Asterix (November) por
Monthly Information Session for MV Asterix (November)Monthly Information Session for MV Asterix (November)
Monthly Information Session for MV Asterix (November)Esquimalt MFRC
98 vistas26 diapositivas
Volf work.pdf por
Volf work.pdfVolf work.pdf
Volf work.pdfMariaKenney3
75 vistas43 diapositivas
Creative Restart 2023: Atila Martins - Craft: A Necessity, Not a Choice por
Creative Restart 2023: Atila Martins - Craft: A Necessity, Not a ChoiceCreative Restart 2023: Atila Martins - Craft: A Necessity, Not a Choice
Creative Restart 2023: Atila Martins - Craft: A Necessity, Not a ChoiceTaste
41 vistas50 diapositivas
ICS3211_lecture 09_2023.pdf por
ICS3211_lecture 09_2023.pdfICS3211_lecture 09_2023.pdf
ICS3211_lecture 09_2023.pdfVanessa Camilleri
134 vistas10 diapositivas
Berry country.pdf por
Berry country.pdfBerry country.pdf
Berry country.pdfMariaKenney3
61 vistas12 diapositivas
Payment Integration using Braintree Connector | MuleSoft Mysore Meetup #37 por
Payment Integration using Braintree Connector | MuleSoft Mysore Meetup #37Payment Integration using Braintree Connector | MuleSoft Mysore Meetup #37
Payment Integration using Braintree Connector | MuleSoft Mysore Meetup #37MysoreMuleSoftMeetup
44 vistas17 diapositivas

Último(20)

Monthly Information Session for MV Asterix (November) por Esquimalt MFRC
Monthly Information Session for MV Asterix (November)Monthly Information Session for MV Asterix (November)
Monthly Information Session for MV Asterix (November)
Esquimalt MFRC98 vistas
Creative Restart 2023: Atila Martins - Craft: A Necessity, Not a Choice por Taste
Creative Restart 2023: Atila Martins - Craft: A Necessity, Not a ChoiceCreative Restart 2023: Atila Martins - Craft: A Necessity, Not a Choice
Creative Restart 2023: Atila Martins - Craft: A Necessity, Not a Choice
Taste41 vistas
Payment Integration using Braintree Connector | MuleSoft Mysore Meetup #37 por MysoreMuleSoftMeetup
Payment Integration using Braintree Connector | MuleSoft Mysore Meetup #37Payment Integration using Braintree Connector | MuleSoft Mysore Meetup #37
Payment Integration using Braintree Connector | MuleSoft Mysore Meetup #37
Narration lesson plan por TARIQ KHAN
Narration lesson planNarration lesson plan
Narration lesson plan
TARIQ KHAN69 vistas
JQUERY.pdf por ArthyR3
JQUERY.pdfJQUERY.pdf
JQUERY.pdf
ArthyR3103 vistas
The Accursed House by Émile Gaboriau por DivyaSheta
The Accursed House  by Émile GaboriauThe Accursed House  by Émile Gaboriau
The Accursed House by Émile Gaboriau
DivyaSheta246 vistas
Parts of Speech (1).pptx por mhkpreet001
Parts of Speech (1).pptxParts of Speech (1).pptx
Parts of Speech (1).pptx
mhkpreet00143 vistas
EILO EXCURSION PROGRAMME 2023 por info33492
EILO EXCURSION PROGRAMME 2023EILO EXCURSION PROGRAMME 2023
EILO EXCURSION PROGRAMME 2023
info33492181 vistas
Career Building in AI - Technologies, Trends and Opportunities por WebStackAcademy
Career Building in AI - Technologies, Trends and OpportunitiesCareer Building in AI - Technologies, Trends and Opportunities
Career Building in AI - Technologies, Trends and Opportunities
WebStackAcademy41 vistas
11.28.23 Social Capital and Social Exclusion.pptx por mary850239
11.28.23 Social Capital and Social Exclusion.pptx11.28.23 Social Capital and Social Exclusion.pptx
11.28.23 Social Capital and Social Exclusion.pptx
mary850239409 vistas

Using cookies and sessions

  • 1. Using Cookies and Sessions By Sana Mateen
  • 2. Cookie vs session cookie • A cookie is a small piece of text stored on a user's computer by their browser. Common uses for cookies are authentication, storing of site preferences, shopping cart items, and server session identification. • Each time the users' web browser interacts with a web server it will pass the cookie information to the web server. Only the cookies stored by the browser that relate to the domain in the requested URL will be sent to the server. This means that cookies that relate to www.example.com will not be sent to www.exampledomain.com. • In essence, a cookie is a great way of linking one page to the next for a user's interaction with a web site or web application. session • A session can be defined as a server- side storage of information that is desired to persist throughout the user's interaction with the web site or web application. • Instead of storing large and constantly changing information via cookies in the user's browser, only a unique identifier is stored on the client side (called a "session id"). This session id is passed to the web server every time the browser makes an HTTP request (ie a page link or AJAX request). The web application pairs this session id with it's internal database and retrieves the stored variables for use by the requested page.
  • 3. • 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. • For Example: • Flipkart uses session to maintain thedetails of various products selected by its customers, which is called cart. • Session Tracking is a way to maintain state (data) of an user. It is also known as session management in servlet.
  • 4. Cookies in servlets • A cookie is a small piece of information that is persisted between the multiple client requests. • 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. • Gmail uses cookie technique for login. If you disable the cookie, gmail won't work.
  • 10. Sessions • Servlet API provides Session management through HttpSession interface. We can get session from HttpServletRequest object using following methods. HttpSession allows us to set objects as attributes that can be retrieved in future requests. • HttpSession getSession() – This method always returns a HttpSession object. It returns the session object attached with the request, if the request has no session attached, then it creates a new session and return it. • HttpSession getSession(boolean flag) – This method returns HttpSession object if request has session else it returns null.
  • 11. • Some of the important methods of HttpSession are: • String getId() – Returns a string containing the unique identifier assigned to this session. • Object getAttribute(String name) – Returns the object bound with the specified name in this session, or null if no object is bound under the name. Some other methods to work with Session attributes are getAttributeNames(), removeAttribute(String name) and setAttribute(String name, Object value). • long getCreationTime() – Returns the time when this session was created, measured in milliseconds since midnight January 1, 1970 GMT. We can get last accessed time with getLastAccessedTime() method. • setMaxInactiveInterval(int interval) – Specifies the time, in seconds, between client requests before the servlet container will invalidate this session. We can get session timeout value from getMaxInactiveInterval() method. • ServletContext getServletContext() – Returns ServletContext object for the application. • boolean isNew() – Returns true if the client does not yet know about the session or if the client chooses not to join the session. • void invalidate() – Invalidates this session then unbinds any objects bound to it.