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
Session And Cookies In Servlets - Java
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
Ad

More Related Content

What's hot (20)

Cookies & Session
Cookies & SessionCookies & Session
Cookies & Session
university of education,Lahore
 
Sessions and cookies
Sessions and cookiesSessions and cookies
Sessions and cookies
www.netgains.org
 
Java beans
Java beansJava beans
Java beans
Rajkiran Mummadi
 
Asp.net state management
Asp.net state managementAsp.net state management
Asp.net state management
priya Nithya
 
JavaScript - Chapter 12 - Document Object Model
  JavaScript - Chapter 12 - Document Object Model  JavaScript - Chapter 12 - Document Object Model
JavaScript - Chapter 12 - Document Object Model
WebStackAcademy
 
jQuery Ajax
jQuery AjaxjQuery Ajax
jQuery Ajax
Anand Kumar Rajana
 
Java Servlets
Java ServletsJava Servlets
Java Servlets
BG Java EE Course
 
Web search Technologies
Web search TechnologiesWeb search Technologies
Web search Technologies
Abdul Sami Kharal
 
Oops concepts in php
Oops concepts in phpOops concepts in php
Oops concepts in php
CPD INDIA
 
Asp .net web form fundamentals
Asp .net web form fundamentalsAsp .net web form fundamentals
Asp .net web form fundamentals
Gopal Ji Singh
 
ASP.NET Page Life Cycle
ASP.NET Page Life CycleASP.NET Page Life Cycle
ASP.NET Page Life Cycle
Abhishek Sur
 
Cookies and sessions
Cookies and sessionsCookies and sessions
Cookies and sessions
Lena Petsenchuk
 
Ajax
AjaxAjax
Ajax
Tech_MX
 
HTTP Request and Response Structure
HTTP Request and Response StructureHTTP Request and Response Structure
HTTP Request and Response Structure
BhagyashreeGajera1
 
Master page in Asp.net
Master page in Asp.netMaster page in Asp.net
Master page in Asp.net
RupinderjitKaur9
 
Servlet life cycle
Servlet life cycleServlet life cycle
Servlet life cycle
Venkateswara Rao N
 
Form Validation in JavaScript
Form Validation in JavaScriptForm Validation in JavaScript
Form Validation in JavaScript
Ravi Bhadauria
 
Javascript validating form
Javascript validating formJavascript validating form
Javascript validating form
Jesus Obenita Jr.
 
PHP - Introduction to PHP AJAX
PHP -  Introduction to PHP AJAXPHP -  Introduction to PHP AJAX
PHP - Introduction to PHP AJAX
Vibrant Technologies & Computers
 
Servletarchitecture,lifecycle,get,post
Servletarchitecture,lifecycle,get,postServletarchitecture,lifecycle,get,post
Servletarchitecture,lifecycle,get,post
vamsi krishna
 

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

Class 38
Class 38Class 38
Class 38
srasat73
 
Enterprise java unit-2_chapter-3
Enterprise  java unit-2_chapter-3Enterprise  java unit-2_chapter-3
Enterprise java unit-2_chapter-3
sandeep54552
 
IMPORTANT SESSION TRACKING TECHNIQUES.pptx
IMPORTANT SESSION TRACKING TECHNIQUES.pptxIMPORTANT SESSION TRACKING TECHNIQUES.pptx
IMPORTANT SESSION TRACKING TECHNIQUES.pptx
yvtinsane
 
Session and state management
Session and state managementSession and state management
Session and state management
Paneliya Prince
 
Servlet sessions
Servlet sessionsServlet sessions
Servlet sessions
vantinhkhuc
 
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
Ecom2
Santosh Pandey
 
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
 
Session,cookies
Session,cookiesSession,cookies
Session,cookies
rkmourya511
 
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
 
chapter-06-servlet_finalppt.pdfSDSADHAJSHD
chapter-06-servlet_finalppt.pdfSDSADHAJSHDchapter-06-servlet_finalppt.pdfSDSADHAJSHD
chapter-06-servlet_finalppt.pdfSDSADHAJSHD
KarishmaTamboli4
 
WEB [email protected]
WEB Mod5@AzDOCUMENTS.in.pdfWEB Mod5@AzDOCUMENTS.in.pdf
WEB [email protected]
PrathimaMahapurush1
 
Session Management & Cookies In Php
Session Management & Cookies In PhpSession Management & Cookies In Php
Session Management & Cookies In Php
Harit Kothari
 
Enterprise java unit-2_chapter-3
Enterprise  java unit-2_chapter-3Enterprise  java unit-2_chapter-3
Enterprise java unit-2_chapter-3
sandeep54552
 
IMPORTANT SESSION TRACKING TECHNIQUES.pptx
IMPORTANT SESSION TRACKING TECHNIQUES.pptxIMPORTANT SESSION TRACKING TECHNIQUES.pptx
IMPORTANT SESSION TRACKING TECHNIQUES.pptx
yvtinsane
 
Session and state management
Session and state managementSession and state management
Session and state management
Paneliya Prince
 
Servlet sessions
Servlet sessionsServlet sessions
Servlet sessions
vantinhkhuc
 
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
 
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
 
chapter-06-servlet_finalppt.pdfSDSADHAJSHD
chapter-06-servlet_finalppt.pdfSDSADHAJSHDchapter-06-servlet_finalppt.pdfSDSADHAJSHD
chapter-06-servlet_finalppt.pdfSDSADHAJSHD
KarishmaTamboli4
 
Session Management & Cookies In Php
Session Management & Cookies In PhpSession Management & Cookies In Php
Session Management & Cookies In Php
Harit Kothari
 
Ad

Recently uploaded (20)

Scaling GraphRAG: Efficient Knowledge Retrieval for Enterprise AI
Scaling GraphRAG:  Efficient Knowledge Retrieval for Enterprise AIScaling GraphRAG:  Efficient Knowledge Retrieval for Enterprise AI
Scaling GraphRAG: Efficient Knowledge Retrieval for Enterprise AI
danshalev
 
Agentic AI Use Cases using GenAI LLM models
Agentic AI Use Cases using GenAI LLM modelsAgentic AI Use Cases using GenAI LLM models
Agentic AI Use Cases using GenAI LLM models
Manish Chopra
 
Secure Test Infrastructure: The Backbone of Trustworthy Software Development
Secure Test Infrastructure: The Backbone of Trustworthy Software DevelopmentSecure Test Infrastructure: The Backbone of Trustworthy Software Development
Secure Test Infrastructure: The Backbone of Trustworthy Software Development
Shubham Joshi
 
Designing AI-Powered APIs on Azure: Best Practices& Considerations
Designing AI-Powered APIs on Azure: Best Practices& ConsiderationsDesigning AI-Powered APIs on Azure: Best Practices& Considerations
Designing AI-Powered APIs on Azure: Best Practices& Considerations
Dinusha Kumarasiri
 
Douwan Crack 2025 new verson+ License code
Douwan Crack 2025 new verson+ License codeDouwan Crack 2025 new verson+ License code
Douwan Crack 2025 new verson+ License code
aneelaramzan63
 
Explaining GitHub Actions Failures with Large Language Models Challenges, In...
Explaining GitHub Actions Failures with Large Language Models Challenges, In...Explaining GitHub Actions Failures with Large Language Models Challenges, In...
Explaining GitHub Actions Failures with Large Language Models Challenges, In...
ssuserb14185
 
How to Batch Export Lotus Notes NSF Emails to Outlook PST Easily?
How to Batch Export Lotus Notes NSF Emails to Outlook PST Easily?How to Batch Export Lotus Notes NSF Emails to Outlook PST Easily?
How to Batch Export Lotus Notes NSF Emails to Outlook PST Easily?
steaveroggers
 
Adobe Photoshop CC 2025 Crack Full Serial Key With Latest
Adobe Photoshop CC 2025 Crack Full Serial Key  With LatestAdobe Photoshop CC 2025 Crack Full Serial Key  With Latest
Adobe Photoshop CC 2025 Crack Full Serial Key With Latest
usmanhidray
 
Download YouTube By Click 2025 Free Full Activated
Download YouTube By Click 2025 Free Full ActivatedDownload YouTube By Click 2025 Free Full Activated
Download YouTube By Click 2025 Free Full Activated
saniamalik72555
 
FL Studio Producer Edition Crack 2025 Full Version
FL Studio Producer Edition Crack 2025 Full VersionFL Studio Producer Edition Crack 2025 Full Version
FL Studio Producer Edition Crack 2025 Full Version
tahirabibi60507
 
Adobe Illustrator Crack FREE Download 2025 Latest Version
Adobe Illustrator Crack FREE Download 2025 Latest VersionAdobe Illustrator Crack FREE Download 2025 Latest Version
Adobe Illustrator Crack FREE Download 2025 Latest Version
kashifyounis067
 
Top 10 Client Portal Software Solutions for 2025.docx
Top 10 Client Portal Software Solutions for 2025.docxTop 10 Client Portal Software Solutions for 2025.docx
Top 10 Client Portal Software Solutions for 2025.docx
Portli
 
Exploring Wayland: A Modern Display Server for the Future
Exploring Wayland: A Modern Display Server for the FutureExploring Wayland: A Modern Display Server for the Future
Exploring Wayland: A Modern Display Server for the Future
ICS
 
Societal challenges of AI: biases, multilinguism and sustainability
Societal challenges of AI: biases, multilinguism and sustainabilitySocietal challenges of AI: biases, multilinguism and sustainability
Societal challenges of AI: biases, multilinguism and sustainability
Jordi Cabot
 
Microsoft AI Nonprofit Use Cases and Live Demo_2025.04.30.pdf
Microsoft AI Nonprofit Use Cases and Live Demo_2025.04.30.pdfMicrosoft AI Nonprofit Use Cases and Live Demo_2025.04.30.pdf
Microsoft AI Nonprofit Use Cases and Live Demo_2025.04.30.pdf
TechSoup
 
How to Optimize Your AWS Environment for Improved Cloud Performance
How to Optimize Your AWS Environment for Improved Cloud PerformanceHow to Optimize Your AWS Environment for Improved Cloud Performance
How to Optimize Your AWS Environment for Improved Cloud Performance
ThousandEyes
 
Meet the Agents: How AI Is Learning to Think, Plan, and Collaborate
Meet the Agents: How AI Is Learning to Think, Plan, and CollaborateMeet the Agents: How AI Is Learning to Think, Plan, and Collaborate
Meet the Agents: How AI Is Learning to Think, Plan, and Collaborate
Maxim Salnikov
 
Download Wondershare Filmora Crack [2025] With Latest
Download Wondershare Filmora Crack [2025] With LatestDownload Wondershare Filmora Crack [2025] With Latest
Download Wondershare Filmora Crack [2025] With Latest
tahirabibi60507
 
Shift Left using Lean for Agile Software Development
Shift Left using Lean for Agile Software DevelopmentShift Left using Lean for Agile Software Development
Shift Left using Lean for Agile Software Development
SathyaShankar6
 
Salesforce Data Cloud- Hyperscale data platform, built for Salesforce.
Salesforce Data Cloud- Hyperscale data platform, built for Salesforce.Salesforce Data Cloud- Hyperscale data platform, built for Salesforce.
Salesforce Data Cloud- Hyperscale data platform, built for Salesforce.
Dele Amefo
 
Scaling GraphRAG: Efficient Knowledge Retrieval for Enterprise AI
Scaling GraphRAG:  Efficient Knowledge Retrieval for Enterprise AIScaling GraphRAG:  Efficient Knowledge Retrieval for Enterprise AI
Scaling GraphRAG: Efficient Knowledge Retrieval for Enterprise AI
danshalev
 
Agentic AI Use Cases using GenAI LLM models
Agentic AI Use Cases using GenAI LLM modelsAgentic AI Use Cases using GenAI LLM models
Agentic AI Use Cases using GenAI LLM models
Manish Chopra
 
Secure Test Infrastructure: The Backbone of Trustworthy Software Development
Secure Test Infrastructure: The Backbone of Trustworthy Software DevelopmentSecure Test Infrastructure: The Backbone of Trustworthy Software Development
Secure Test Infrastructure: The Backbone of Trustworthy Software Development
Shubham Joshi
 
Designing AI-Powered APIs on Azure: Best Practices& Considerations
Designing AI-Powered APIs on Azure: Best Practices& ConsiderationsDesigning AI-Powered APIs on Azure: Best Practices& Considerations
Designing AI-Powered APIs on Azure: Best Practices& Considerations
Dinusha Kumarasiri
 
Douwan Crack 2025 new verson+ License code
Douwan Crack 2025 new verson+ License codeDouwan Crack 2025 new verson+ License code
Douwan Crack 2025 new verson+ License code
aneelaramzan63
 
Explaining GitHub Actions Failures with Large Language Models Challenges, In...
Explaining GitHub Actions Failures with Large Language Models Challenges, In...Explaining GitHub Actions Failures with Large Language Models Challenges, In...
Explaining GitHub Actions Failures with Large Language Models Challenges, In...
ssuserb14185
 
How to Batch Export Lotus Notes NSF Emails to Outlook PST Easily?
How to Batch Export Lotus Notes NSF Emails to Outlook PST Easily?How to Batch Export Lotus Notes NSF Emails to Outlook PST Easily?
How to Batch Export Lotus Notes NSF Emails to Outlook PST Easily?
steaveroggers
 
Adobe Photoshop CC 2025 Crack Full Serial Key With Latest
Adobe Photoshop CC 2025 Crack Full Serial Key  With LatestAdobe Photoshop CC 2025 Crack Full Serial Key  With Latest
Adobe Photoshop CC 2025 Crack Full Serial Key With Latest
usmanhidray
 
Download YouTube By Click 2025 Free Full Activated
Download YouTube By Click 2025 Free Full ActivatedDownload YouTube By Click 2025 Free Full Activated
Download YouTube By Click 2025 Free Full Activated
saniamalik72555
 
FL Studio Producer Edition Crack 2025 Full Version
FL Studio Producer Edition Crack 2025 Full VersionFL Studio Producer Edition Crack 2025 Full Version
FL Studio Producer Edition Crack 2025 Full Version
tahirabibi60507
 
Adobe Illustrator Crack FREE Download 2025 Latest Version
Adobe Illustrator Crack FREE Download 2025 Latest VersionAdobe Illustrator Crack FREE Download 2025 Latest Version
Adobe Illustrator Crack FREE Download 2025 Latest Version
kashifyounis067
 
Top 10 Client Portal Software Solutions for 2025.docx
Top 10 Client Portal Software Solutions for 2025.docxTop 10 Client Portal Software Solutions for 2025.docx
Top 10 Client Portal Software Solutions for 2025.docx
Portli
 
Exploring Wayland: A Modern Display Server for the Future
Exploring Wayland: A Modern Display Server for the FutureExploring Wayland: A Modern Display Server for the Future
Exploring Wayland: A Modern Display Server for the Future
ICS
 
Societal challenges of AI: biases, multilinguism and sustainability
Societal challenges of AI: biases, multilinguism and sustainabilitySocietal challenges of AI: biases, multilinguism and sustainability
Societal challenges of AI: biases, multilinguism and sustainability
Jordi Cabot
 
Microsoft AI Nonprofit Use Cases and Live Demo_2025.04.30.pdf
Microsoft AI Nonprofit Use Cases and Live Demo_2025.04.30.pdfMicrosoft AI Nonprofit Use Cases and Live Demo_2025.04.30.pdf
Microsoft AI Nonprofit Use Cases and Live Demo_2025.04.30.pdf
TechSoup
 
How to Optimize Your AWS Environment for Improved Cloud Performance
How to Optimize Your AWS Environment for Improved Cloud PerformanceHow to Optimize Your AWS Environment for Improved Cloud Performance
How to Optimize Your AWS Environment for Improved Cloud Performance
ThousandEyes
 
Meet the Agents: How AI Is Learning to Think, Plan, and Collaborate
Meet the Agents: How AI Is Learning to Think, Plan, and CollaborateMeet the Agents: How AI Is Learning to Think, Plan, and Collaborate
Meet the Agents: How AI Is Learning to Think, Plan, and Collaborate
Maxim Salnikov
 
Download Wondershare Filmora Crack [2025] With Latest
Download Wondershare Filmora Crack [2025] With LatestDownload Wondershare Filmora Crack [2025] With Latest
Download Wondershare Filmora Crack [2025] With Latest
tahirabibi60507
 
Shift Left using Lean for Agile Software Development
Shift Left using Lean for Agile Software DevelopmentShift Left using Lean for Agile Software Development
Shift Left using Lean for Agile Software Development
SathyaShankar6
 
Salesforce Data Cloud- Hyperscale data platform, built for Salesforce.
Salesforce Data Cloud- Hyperscale data platform, built for Salesforce.Salesforce Data Cloud- Hyperscale data platform, built for Salesforce.
Salesforce Data Cloud- Hyperscale data platform, built for Salesforce.
Dele Amefo
 
Ad

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
  • 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.