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

More Related Content

PDF
Servlet Filter
PPTX
Session tracking in servlets
PPT
Java Servlets
PPT
Jsp ppt
PPTX
Java RMI
PPT
Servlets
PPTX
Chapter 3 servlet & jsp
PPT
Cookies in servlet
Servlet Filter
Session tracking in servlets
Java Servlets
Jsp ppt
Java RMI
Servlets
Chapter 3 servlet & jsp
Cookies in servlet

What's hot (20)

PPT
Jsp/Servlet
PPTX
Java script
PDF
Spring Web Services: SOAP vs. REST
PPTX
Servlets
PPT
JDBC – Java Database Connectivity
PPTX
PPTX
Java beans
PPT
Java Persistence API (JPA) Step By Step
PPTX
Java Server Pages(jsp)
PPTX
PHP FUNCTIONS
PPT
Hibernate architecture
PDF
Introduction to Bootstrap
PPTX
Node.js File system & Streams
PPTX
java Servlet technology
PPTX
Java Server Pages
PPS
Java Hibernate Programming with Architecture Diagram and Example
PPTX
Node.js Express
Jsp/Servlet
Java script
Spring Web Services: SOAP vs. REST
Servlets
JDBC – Java Database Connectivity
Java beans
Java Persistence API (JPA) Step By Step
Java Server Pages(jsp)
PHP FUNCTIONS
Hibernate architecture
Introduction to Bootstrap
Node.js File system & Streams
java Servlet technology
Java Server Pages
Java Hibernate Programming with Architecture Diagram and Example
Node.js Express
Ad

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

PPTX
IMPORTANT SESSION TRACKING TECHNIQUES.pptx
PPTX
Enterprise java unit-2_chapter-3
PPTX
Session 33 - Session Management using other Techniques
PPTX
SessionTrackServlets.pptx
PPTX
Session 32 - Session Management using Cookies
PPTX
Enterprise java unit-2_chapter-2
PPTX
Session tracking In Java
PPT
session and cookies.ppt
PDF
Servlet sessions
PPT
State management
PDF
4.4 PHP Session
PPTX
SCWCD : Session management : CHAP : 6
PPTX
Advance java session 7
PPT
PPTX
Advance java session 8
PPTX
Class 38
PPTX
Using cookies and sessions
PPTX
19_JavaScript - Storage_Cookies-tutorial .pptx
DOCX
State management servlet
PPTX
Session & Cookies
IMPORTANT SESSION TRACKING TECHNIQUES.pptx
Enterprise java unit-2_chapter-3
Session 33 - Session Management using other Techniques
SessionTrackServlets.pptx
Session 32 - Session Management using Cookies
Enterprise java unit-2_chapter-2
Session tracking In Java
session and cookies.ppt
Servlet sessions
State management
4.4 PHP Session
SCWCD : Session management : CHAP : 6
Advance java session 7
Advance java session 8
Class 38
Using cookies and sessions
19_JavaScript - Storage_Cookies-tutorial .pptx
State management servlet
Session & Cookies
Ad

Recently uploaded (20)

PPTX
Computer Software and OS of computer science of grade 11.pptx
PDF
Wondershare Filmora 15 Crack With Activation Key [2025
PDF
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
PDF
top salesforce developer skills in 2025.pdf
PPTX
assetexplorer- product-overview - presentation
PDF
Softaken Excel to vCard Converter Software.pdf
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
PDF
iTop VPN Free 5.6.0.5262 Crack latest version 2025
PPTX
CHAPTER 2 - PM Management and IT Context
PDF
wealthsignaloriginal-com-DS-text-... (1).pdf
PPTX
Reimagine Home Health with the Power of Agentic AI​
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PPTX
Embracing Complexity in Serverless! GOTO Serverless Bengaluru
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PDF
System and Network Administration Chapter 2
PPTX
Transform Your Business with a Software ERP System
PDF
Digital Systems & Binary Numbers (comprehensive )
PDF
Product Update: Alluxio AI 3.7 Now with Sub-Millisecond Latency
Computer Software and OS of computer science of grade 11.pptx
Wondershare Filmora 15 Crack With Activation Key [2025
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
top salesforce developer skills in 2025.pdf
assetexplorer- product-overview - presentation
Softaken Excel to vCard Converter Software.pdf
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
iTop VPN Free 5.6.0.5262 Crack latest version 2025
CHAPTER 2 - PM Management and IT Context
wealthsignaloriginal-com-DS-text-... (1).pdf
Reimagine Home Health with the Power of Agentic AI​
Adobe Illustrator 28.6 Crack My Vision of Vector Design
Internet Downloader Manager (IDM) Crack 6.42 Build 41
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
Embracing Complexity in Serverless! GOTO Serverless Bengaluru
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
System and Network Administration Chapter 2
Transform Your Business with a Software ERP System
Digital Systems & Binary Numbers (comprehensive )
Product Update: Alluxio AI 3.7 Now with Sub-Millisecond Latency

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.