SlideShare a Scribd company logo
Session And Cookies
In Servlets
Table of Contents
1. Servlets
2. The Problem with HTTP
3. Session Tracking in Servlet
4. Cookies
5. Hidden Form Field
6. URL Rewriting
7. HttpSession
8. Session v/s Cookies
What are Servlets anyway?
� Java program that runs on servers.
� Capable of Handling Requests and generating Dynamic Response.
The PROBLEM with HTTP
� HTTP is used as Protocol to transfer data and
information between Client and Server.
� HTTP (Hypertext Transfer Protocol) is STATELESS.
� Client - Server Architecture : A Client requests a
Server and the Server responses a dynamic page
(HTML) when a Servlet processes the requests.
� Server treats every request as a new request as the
state (data) of the user is not saved / maintained.
� Server won’t remember anything from the first
request and does the same task for the new request
even if the user is same as the previous one.
Analogy : The University Admission (Stateless)
Without Session - Stateless Management
Live Demonstration
Session Tracking in Servlet
● Session Tracking is a way to maintain state (data) of an user.
● It is also known as State Management.
Techniques :
1. Cookies
2. Hidden Form Field
3. URL Rewriting
4. HttpSession
Cookies
What are Cookies anyway?
� Small piece of textual information stored in Key-Value pair in Client’s
Browser.
� Cookie is stored in browser’s cache.
How Does Cookie Work?
1. User Signs Up. Client
posts a HTTP request to the
server containing username
and password.
2. Server receives this
request and hashes the
password before storing into
database.
3. Client logs in. Provides
username and password
and again a HTTP request
is posted to server.
4. Server looks up the
username in the database,
hashes the supplied login
password, and compares it
to the previously hashed
password in the database.
5. If the credentials are
correct, server creates an
Access Token, which uniquely
identifies the user’s session.
6. We then store the access
token in the database
associated with that user.
7. Attach the access token
with a Cookie returned to
client. Now the Cookie has
been returned to client and
client stores the cookie in
browser.
8. On client side now, we
are logged in. Every time
now a client makes a
request for a page that
requires authorization (i.e.
they need to be logged in),
the server obtains the
access token from the
cookie and checks it
against the one in the
database associated with
that user. If it checks out,
access is granted.
Live Demonstration
Hidden Form Field
How does Hidden Form Field Works?
� A hidden text field is used for maintaining state of an user.
� We have form in all pages that can be submitted and we can pass user’s data
in an hidden field.
� Does not have to be dependent on browser.
� Works, even if the cookies are disabled.
� Extra Form Submission is required to maintain state.
� Not Ideal.
Live Demonstration
URL Rewriting
How does URL Rewriting Works?
� We append a query string or token to the URL of the next servlet or the next
page.
� It will be a name - value pair.
� If multiple data has to be sent then it will be separated by ampersand(&).
� Works even if the cookies are disabled.
� No extra form submission.
� Works with links or hyperlinks.
Live Demonstration
HttpSession
How does HttpSession Works?
� Session simply means small interval of time.
� Used for state management.
� When a client requests a server for the first time, the server creates a Session
ID, and stores it with some key value pair like client’s name, email, photo, etc.
� When the client again requests to the server, the server checks the session, if
not expired or destroyed, and allows the client to do tasks, without getting to
logged in again.
� Session expires in three cases:
1. Closing the Browser
2. Time expired
3. Invalidate
Live Demonstration
between
Cookies and Session
Session
� Stores variables in temporary directory in
server.
� Ends when user logout’s or browser closes.
� Stores unlimited amount of data.
� A script can use maximum 128 MB.
� “req.getSession(true)” to create a new
session. “req.getSession(false)” to get the
already set session.
� “session.setAttribute(key, value)” to set
session values.
� “session.invalidate()” or
“session.setMaxInactiveInterval(seconds)
” to destroy session.
� Sessions are more secured as they are
stored in server and encrypted form.
Cookies
� Stores in Client’s (browser).
� Ends on the lifetime set by user.
� Stores limited data.
� Maximum size of Browser’s cookies is 4 KB.
� “new Cookie(key, value)” to create a new
cookie.
� “resp.addCookie(c)” to add the cookie in
the response.
� “c.setMaxAge(seconds)” to set the expiry
time of cookie.
� Cookies are not secured as data is in textual
format and it gets stored in client machine.
Session And Cookies In Servlets - Java

More Related Content

What's hot

Java Server Pages(jsp)
Java Server Pages(jsp)Java Server Pages(jsp)
Java Server Pages(jsp)
Manisha Keim
 
jQuery Ajax
jQuery AjaxjQuery Ajax
jQuery Ajax
Anand Kumar Rajana
 
Introduction to ajax
Introduction  to  ajaxIntroduction  to  ajax
Introduction to ajax
Pihu Goel
 
Advance Java Topics (J2EE)
Advance Java Topics (J2EE)Advance Java Topics (J2EE)
Advance Java Topics (J2EE)
slire
 
Http request and http response
Http request and http responseHttp request and http response
Http request and http response
Nuha Noor
 
Javascript
JavascriptJavascript
Javascript
Manav Prasad
 
Java J2EE
Java J2EEJava J2EE
Java J2EE
Sandeep Rawat
 
android activity
android activityandroid activity
android activityDeepa Rani
 
Jsp/Servlet
Jsp/ServletJsp/Servlet
Jsp/Servlet
Sunil OS
 
Java PRACTICAL file
Java PRACTICAL fileJava PRACTICAL file
Java PRACTICAL file
RACHIT_GUPTA
 
Angular - Chapter 9 - Authentication and Authorization
Angular - Chapter 9 - Authentication and AuthorizationAngular - Chapter 9 - Authentication and Authorization
Angular - Chapter 9 - Authentication and Authorization
WebStackAcademy
 
Asynchronous JavaScript & XML (AJAX)
Asynchronous JavaScript & XML (AJAX)Asynchronous JavaScript & XML (AJAX)
Asynchronous JavaScript & XML (AJAX)
Adnan Sohail
 
Ppt on sql injection
Ppt on sql injectionPpt on sql injection
Ppt on sql injectionashish20012
 
File system node js
File system node jsFile system node js
File system node js
monikadeshmane
 
Java/Servlet/JSP/JDBC
Java/Servlet/JSP/JDBCJava/Servlet/JSP/JDBC
Java/Servlet/JSP/JDBC
FAKHRUN NISHA
 
jQuery PPT
jQuery PPTjQuery PPT
jQuery PPT
Dominic Arrojado
 
JavaScript - Chapter 11 - Events
 JavaScript - Chapter 11 - Events  JavaScript - Chapter 11 - Events
JavaScript - Chapter 11 - Events
WebStackAcademy
 

What's hot (20)

Java Server Pages(jsp)
Java Server Pages(jsp)Java Server Pages(jsp)
Java Server Pages(jsp)
 
jQuery Ajax
jQuery AjaxjQuery Ajax
jQuery Ajax
 
Introduction to ajax
Introduction  to  ajaxIntroduction  to  ajax
Introduction to ajax
 
Advance Java Topics (J2EE)
Advance Java Topics (J2EE)Advance Java Topics (J2EE)
Advance Java Topics (J2EE)
 
Http request and http response
Http request and http responseHttp request and http response
Http request and http response
 
Javascript
JavascriptJavascript
Javascript
 
Java J2EE
Java J2EEJava J2EE
Java J2EE
 
android activity
android activityandroid activity
android activity
 
Java script
Java scriptJava script
Java script
 
Jsp/Servlet
Jsp/ServletJsp/Servlet
Jsp/Servlet
 
Java PRACTICAL file
Java PRACTICAL fileJava PRACTICAL file
Java PRACTICAL file
 
Angular - Chapter 9 - Authentication and Authorization
Angular - Chapter 9 - Authentication and AuthorizationAngular - Chapter 9 - Authentication and Authorization
Angular - Chapter 9 - Authentication and Authorization
 
Asynchronous JavaScript & XML (AJAX)
Asynchronous JavaScript & XML (AJAX)Asynchronous JavaScript & XML (AJAX)
Asynchronous JavaScript & XML (AJAX)
 
Ppt on sql injection
Ppt on sql injectionPpt on sql injection
Ppt on sql injection
 
File system node js
File system node jsFile system node js
File system node js
 
Asp Architecture
Asp ArchitectureAsp Architecture
Asp Architecture
 
Java/Servlet/JSP/JDBC
Java/Servlet/JSP/JDBCJava/Servlet/JSP/JDBC
Java/Servlet/JSP/JDBC
 
jQuery PPT
jQuery PPTjQuery PPT
jQuery PPT
 
Asp.net.
Asp.net.Asp.net.
Asp.net.
 
JavaScript - Chapter 11 - Events
 JavaScript - Chapter 11 - Events  JavaScript - Chapter 11 - Events
JavaScript - Chapter 11 - Events
 

Similar to Session And Cookies In Servlets - Java

Enterprise java unit-2_chapter-3
Enterprise  java unit-2_chapter-3Enterprise  java unit-2_chapter-3
Enterprise java unit-2_chapter-3
sandeep54552
 
Session and state management
Session and state managementSession and state management
Session and state management
Paneliya Prince
 
Servlet sessions
Servlet sessionsServlet sessions
Servlet sessionsvantinhkhuc
 
Session tracking In Java
Session tracking In JavaSession tracking In Java
Session tracking In Java
honeyvachharajani
 
EAI design patterns/best practices
EAI design patterns/best practicesEAI design patterns/best practices
EAI design patterns/best practices
Ajit Bhingarkar
 
java Servlet technology
java Servlet technologyjava Servlet technology
java Servlet technology
Tanmoy Barman
 
session and cookies.ppt
session and cookies.pptsession and cookies.ppt
session and cookies.ppt
Jayaprasanna4
 
State management
State managementState management
State management
Lalit Kale
 
Ecom2
Ecom2Ecom2
It and ej
It and ejIt and ej
It and ej
Harihar Kalia
 
Using cookies and sessions
Using cookies and sessionsUsing cookies and sessions
Using cookies and sessions
Nuha Noor
 
19_JavaScript - Storage_Cookies-tutorial .pptx
19_JavaScript - Storage_Cookies-tutorial .pptx19_JavaScript - Storage_Cookies-tutorial .pptx
19_JavaScript - Storage_Cookies-tutorial .pptx
ssuser4a97d3
 
Jsp session tracking
Jsp   session trackingJsp   session tracking
Jsp session tracking
rvarshneyp
 
Session tracking in servlets
Session tracking in servletsSession tracking in servlets
Session tracking in servlets
vishal choudhary
 
WEB Mod5@AzDOCUMENTS.in.pdf
WEB Mod5@AzDOCUMENTS.in.pdfWEB Mod5@AzDOCUMENTS.in.pdf
WEB Mod5@AzDOCUMENTS.in.pdf
PrathimaMahapurush1
 
Session Management & Cookies In Php
Session Management & Cookies In PhpSession Management & Cookies In Php
Session Management & Cookies In PhpHarit Kothari
 
State management servlet
State management servletState management servlet
State management servlet
Thakur Amit Tomer
 
Difference between Client Polling vs Server Push vs Websocket vs Long Polling
Difference between Client Polling vs Server Push vs Websocket vs Long PollingDifference between Client Polling vs Server Push vs Websocket vs Long Polling
Difference between Client Polling vs Server Push vs Websocket vs Long Polling
jeetendra mandal
 

Similar to Session And Cookies In Servlets - Java (20)

Class 38
Class 38Class 38
Class 38
 
Enterprise java unit-2_chapter-3
Enterprise  java unit-2_chapter-3Enterprise  java unit-2_chapter-3
Enterprise java unit-2_chapter-3
 
Session and state management
Session and state managementSession and state management
Session and state management
 
Servlet sessions
Servlet sessionsServlet sessions
Servlet sessions
 
Session tracking In Java
Session tracking In JavaSession tracking In Java
Session tracking In Java
 
EAI design patterns/best practices
EAI design patterns/best practicesEAI design patterns/best practices
EAI design patterns/best practices
 
java Servlet technology
java Servlet technologyjava Servlet technology
java Servlet technology
 
session and cookies.ppt
session and cookies.pptsession and cookies.ppt
session and cookies.ppt
 
State management
State managementState management
State management
 
Ecom2
Ecom2Ecom2
Ecom2
 
It and ej
It and ejIt and ej
It and ej
 
Using cookies and sessions
Using cookies and sessionsUsing cookies and sessions
Using cookies and sessions
 
Session,cookies
Session,cookiesSession,cookies
Session,cookies
 
19_JavaScript - Storage_Cookies-tutorial .pptx
19_JavaScript - Storage_Cookies-tutorial .pptx19_JavaScript - Storage_Cookies-tutorial .pptx
19_JavaScript - Storage_Cookies-tutorial .pptx
 
Jsp session tracking
Jsp   session trackingJsp   session tracking
Jsp session tracking
 
Session tracking in servlets
Session tracking in servletsSession tracking in servlets
Session tracking in servlets
 
WEB Mod5@AzDOCUMENTS.in.pdf
WEB Mod5@AzDOCUMENTS.in.pdfWEB Mod5@AzDOCUMENTS.in.pdf
WEB Mod5@AzDOCUMENTS.in.pdf
 
Session Management & Cookies In Php
Session Management & Cookies In PhpSession Management & Cookies In Php
Session Management & Cookies In Php
 
State management servlet
State management servletState management servlet
State management servlet
 
Difference between Client Polling vs Server Push vs Websocket vs Long Polling
Difference between Client Polling vs Server Push vs Websocket vs Long PollingDifference between Client Polling vs Server Push vs Websocket vs Long Polling
Difference between Client Polling vs Server Push vs Websocket vs Long Polling
 

Recently uploaded

Cyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdfCyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdf
Cyanic lab
 
Enhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdfEnhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdf
Globus
 
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Shahin Sheidaei
 
Strategies for Successful Data Migration Tools.pptx
Strategies for Successful Data Migration Tools.pptxStrategies for Successful Data Migration Tools.pptx
Strategies for Successful Data Migration Tools.pptx
varshanayak241
 
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus
 
A Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdfA Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdf
kalichargn70th171
 
How Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptxHow Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptx
wottaspaceseo
 
Visitor Management System in India- Vizman.app
Visitor Management System in India- Vizman.appVisitor Management System in India- Vizman.app
Visitor Management System in India- Vizman.app
NaapbooksPrivateLimi
 
Explore Modern SharePoint Templates for 2024
Explore Modern SharePoint Templates for 2024Explore Modern SharePoint Templates for 2024
Explore Modern SharePoint Templates for 2024
Sharepoint Designs
 
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Anthony Dahanne
 
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, BetterWebinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
XfilesPro
 
First Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User EndpointsFirst Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User Endpoints
Globus
 
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Globus
 
Using IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New ZealandUsing IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New Zealand
IES VE
 
Accelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with PlatformlessAccelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with Platformless
WSO2
 
Advanced Flow Concepts Every Developer Should Know
Advanced Flow Concepts Every Developer Should KnowAdvanced Flow Concepts Every Developer Should Know
Advanced Flow Concepts Every Developer Should Know
Peter Caitens
 
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
informapgpstrackings
 
Multiple Your Crypto Portfolio with the Innovative Features of Advanced Crypt...
Multiple Your Crypto Portfolio with the Innovative Features of Advanced Crypt...Multiple Your Crypto Portfolio with the Innovative Features of Advanced Crypt...
Multiple Your Crypto Portfolio with the Innovative Features of Advanced Crypt...
Hivelance Technology
 
Lecture 1 Introduction to games development
Lecture 1 Introduction to games developmentLecture 1 Introduction to games development
Lecture 1 Introduction to games development
abdulrafaychaudhry
 
Designing for Privacy in Amazon Web Services
Designing for Privacy in Amazon Web ServicesDesigning for Privacy in Amazon Web Services
Designing for Privacy in Amazon Web Services
KrzysztofKkol1
 

Recently uploaded (20)

Cyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdfCyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdf
 
Enhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdfEnhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdf
 
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
 
Strategies for Successful Data Migration Tools.pptx
Strategies for Successful Data Migration Tools.pptxStrategies for Successful Data Migration Tools.pptx
Strategies for Successful Data Migration Tools.pptx
 
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024
 
A Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdfA Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdf
 
How Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptxHow Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptx
 
Visitor Management System in India- Vizman.app
Visitor Management System in India- Vizman.appVisitor Management System in India- Vizman.app
Visitor Management System in India- Vizman.app
 
Explore Modern SharePoint Templates for 2024
Explore Modern SharePoint Templates for 2024Explore Modern SharePoint Templates for 2024
Explore Modern SharePoint Templates for 2024
 
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
 
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, BetterWebinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
 
First Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User EndpointsFirst Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User Endpoints
 
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
 
Using IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New ZealandUsing IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New Zealand
 
Accelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with PlatformlessAccelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with Platformless
 
Advanced Flow Concepts Every Developer Should Know
Advanced Flow Concepts Every Developer Should KnowAdvanced Flow Concepts Every Developer Should Know
Advanced Flow Concepts Every Developer Should Know
 
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
 
Multiple Your Crypto Portfolio with the Innovative Features of Advanced Crypt...
Multiple Your Crypto Portfolio with the Innovative Features of Advanced Crypt...Multiple Your Crypto Portfolio with the Innovative Features of Advanced Crypt...
Multiple Your Crypto Portfolio with the Innovative Features of Advanced Crypt...
 
Lecture 1 Introduction to games development
Lecture 1 Introduction to games developmentLecture 1 Introduction to games development
Lecture 1 Introduction to games development
 
Designing for Privacy in Amazon Web Services
Designing for Privacy in Amazon Web ServicesDesigning for Privacy in Amazon Web Services
Designing for Privacy in Amazon Web Services
 

Session And Cookies In Servlets - Java

  • 2. Table of Contents 1. Servlets 2. The Problem with HTTP 3. Session Tracking in Servlet 4. Cookies 5. Hidden Form Field 6. URL Rewriting 7. HttpSession 8. Session v/s Cookies
  • 3. What are Servlets anyway? � Java program that runs on servers. � Capable of Handling Requests and generating Dynamic Response.
  • 4. The PROBLEM with HTTP � HTTP is used as Protocol to transfer data and information between Client and Server. � HTTP (Hypertext Transfer Protocol) is STATELESS. � Client - Server Architecture : A Client requests a Server and the Server responses a dynamic page (HTML) when a Servlet processes the requests. � Server treats every request as a new request as the state (data) of the user is not saved / maintained. � Server won’t remember anything from the first request and does the same task for the new request even if the user is same as the previous one.
  • 5. Analogy : The University Admission (Stateless)
  • 6. Without Session - Stateless Management
  • 8. Session Tracking in Servlet ● Session Tracking is a way to maintain state (data) of an user. ● It is also known as State Management. Techniques : 1. Cookies 2. Hidden Form Field 3. URL Rewriting 4. HttpSession
  • 10. What are Cookies anyway? � Small piece of textual information stored in Key-Value pair in Client’s Browser. � Cookie is stored in browser’s cache.
  • 12. 1. User Signs Up. Client posts a HTTP request to the server containing username and password. 2. Server receives this request and hashes the password before storing into database.
  • 13. 3. Client logs in. Provides username and password and again a HTTP request is posted to server. 4. Server looks up the username in the database, hashes the supplied login password, and compares it to the previously hashed password in the database.
  • 14. 5. If the credentials are correct, server creates an Access Token, which uniquely identifies the user’s session. 6. We then store the access token in the database associated with that user.
  • 15. 7. Attach the access token with a Cookie returned to client. Now the Cookie has been returned to client and client stores the cookie in browser.
  • 16. 8. On client side now, we are logged in. Every time now a client makes a request for a page that requires authorization (i.e. they need to be logged in), the server obtains the access token from the cookie and checks it against the one in the database associated with that user. If it checks out, access is granted.
  • 19. How does Hidden Form Field Works? � A hidden text field is used for maintaining state of an user. � We have form in all pages that can be submitted and we can pass user’s data in an hidden field. � Does not have to be dependent on browser. � Works, even if the cookies are disabled. � Extra Form Submission is required to maintain state. � Not Ideal.
  • 22. How does URL Rewriting Works? � We append a query string or token to the URL of the next servlet or the next page. � It will be a name - value pair. � If multiple data has to be sent then it will be separated by ampersand(&). � Works even if the cookies are disabled. � No extra form submission. � Works with links or hyperlinks.
  • 25. How does HttpSession Works? � Session simply means small interval of time. � Used for state management. � When a client requests a server for the first time, the server creates a Session ID, and stores it with some key value pair like client’s name, email, photo, etc. � When the client again requests to the server, the server checks the session, if not expired or destroyed, and allows the client to do tasks, without getting to logged in again. � Session expires in three cases: 1. Closing the Browser 2. Time expired 3. Invalidate
  • 26.
  • 29. Session � Stores variables in temporary directory in server. � Ends when user logout’s or browser closes. � Stores unlimited amount of data. � A script can use maximum 128 MB. � “req.getSession(true)” to create a new session. “req.getSession(false)” to get the already set session. � “session.setAttribute(key, value)” to set session values. � “session.invalidate()” or “session.setMaxInactiveInterval(seconds) ” to destroy session. � Sessions are more secured as they are stored in server and encrypted form. Cookies � Stores in Client’s (browser). � Ends on the lifetime set by user. � Stores limited data. � Maximum size of Browser’s cookies is 4 KB. � “new Cookie(key, value)” to create a new cookie. � “resp.addCookie(c)” to add the cookie in the response. � “c.setMaxAge(seconds)” to set the expiry time of cookie. � Cookies are not secured as data is in textual format and it gets stored in client machine.