SlideShare a Scribd company logo
Cookies
Agenda
• Stateless nature of HTTP Protocol
• Explain the need for Session
• Understanding the benefits and drawback of cookies
• Sending Outgoing cookies
• Receiving Incoming cookies
• Tracking repeat visitors
• Specifying cookie attributes
HTTP Protocol
• Stateless Protocol
- Communication protocol is stateless
- Communication consist of independent pairs of request and response
- IP and HTTP are stateless protocols
• Advantages of stateless protocol
- No need to dynamically allocate storage for conversation
- No clean up activity if request dies mid way
HTTP Protocol
• Cookie is a small text file which get stored on the client side
• Cookie contains name and value pair
• Cookie is stored only for a particular duration after which they are
expired
• Client returns the same name and value when it connects to the
- Same site
- Same domains
- Depends upon cookie setting
HTTP Protocol
• Typical Uses of Cookies
- Identifying a user during an e-commerce session
- Servlets have a higher-level API for this task
- Avoiding username and password
- Customizing a site
- Focusing advertising
Problems with Cookies
• The problem is privacy, not security.
- Servers can remember your previous actions
- If you give out personal information, servers can link that information to
your previous actions
- Servers can share cookie information through use of a cooperating third
party like doubleclick.net
- Poorly designed sites store sensitive information like credit card
numbers directly in cookie
- JavaScript bugs let hostile sites steal cookies (old browsers)
Problems with Cookies
• The problem is privacy, not security.
- Moral for servlet authors
• If cookies are not critical to your task, avoid servlets that totally fail
when cookies are disabled
• Don't put sensitive info in cookies
Sending a simple Cookie
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ServletDemo extends HttpServlet{
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException{
response.setContentType("text/html");
PrintWriter pw = response.getWriter();
Cookie cookie = new Cookie(“name",”talentedge”);
cookie.setMaxAge(60*60); //1 hour
response.addCookie(cookie);
pw.println("Cookies created");
} }
Reading Cookies
public static String getCookieValue(Cookie[] cookies, String cookieName,
String defaultValue)
{
for(int i=0; i<cookies.length; i++)
{
Cookie cookie = cookies[i];
if (cookieName.equals(cookie.getName()))
return(cookie.getValue());
}
return(defaultValue);
}
Cookie Method
Cookie Method
Summary
• Creates a cookie, a small amount of information sent by a servlet to a Web browser,
saved by the browser, and later sent back to the server. A cookie's value can uniquely
identify a client, so cookies are commonly used for session management.
• A cookie has a name, a single value, and optional attributes such as a comment, path and
domain qualifiers, a maximum age, and a version number. Some Web browsers have
bugs in how they handle the optional attributes, so use them sparingly to improve the
interoperability of your servlets.
Summary
• The servlet sends cookies to the browser by using the
HttpServletResponse.addCookie(javax.servlet.http.Cookie) method, which adds fields to
HTTP response headers to send cookies to the browser, one at a time. The browser is
expected to support 20 cookies for each Web server, 300 cookies total, and may limit
cookie size to 4 KB each.
• The browser returns cookies to the servlet by adding fields to HTTP request headers.
Cookies can be retrieved from a request by using the HttpServletRequest.getCookies()
method. Several cookies might have the same name but different path attributes.

More Related Content

What's hot (20)

PPTX
Sqlviking
Jonn Callahan
 
PDF
[Cluj] CSP (Content Security Policy)
OWASP EEE
 
PPT
jclouds overview
Adrian Cole
 
PPT
Node js
umesh patil
 
PDF
Is OSGi Modularity Always Worth It? - Glyn Normington
mfrancis
 
PPTX
Java Session
AathikaJava
 
PPTX
Azure Low Lands 2019 - Building secure cloud applications with Azure Key Vault
Tom Kerkhove
 
PDF
https://docs.google.com/presentation/d/1DcL4zK6i3HZRDD4xTGX1VpSOwyu2xBeWLT6a_...
MongoDB
 
PPTX
The Azure API-ness Factory
Mike Martin
 
PPTX
Protecting your online and on premises assets “Cloud Style”
Mike Martin
 
KEY
RocketJS Nodejs rapid development framework for production web apps
wavome
 
PPTX
Micro Web Service - Slim and JWT
Tuyen Vuong
 
PPTX
Caching in asp.net mvc
KarthikaDevi Rajasekaran
 
PPTX
ITProceed 2015 - Securing Sensitive Data with Azure Key Vault
Tom Kerkhove
 
PDF
Ускоряем загрузку картинок вебсокетами
2ГИС Технологии
 
PPSX
Azure DocumentDB
Shiju Varghese
 
PPTX
Magento performance feat. core Hacks
Daniel Niedergesäß
 
PDF
What's New in Nuxeo Platform 7.3
Nuxeo
 
PPTX
Azure key vault - Brisbane User Group
Rahul Nath
 
PDF
Péhápkaři v Pecce: Jak na bezpečnostní hlavičky – Marek Humpolík – 23. 1. 2019
PeckaDesign.cz
 
Sqlviking
Jonn Callahan
 
[Cluj] CSP (Content Security Policy)
OWASP EEE
 
jclouds overview
Adrian Cole
 
Node js
umesh patil
 
Is OSGi Modularity Always Worth It? - Glyn Normington
mfrancis
 
Java Session
AathikaJava
 
Azure Low Lands 2019 - Building secure cloud applications with Azure Key Vault
Tom Kerkhove
 
https://docs.google.com/presentation/d/1DcL4zK6i3HZRDD4xTGX1VpSOwyu2xBeWLT6a_...
MongoDB
 
The Azure API-ness Factory
Mike Martin
 
Protecting your online and on premises assets “Cloud Style”
Mike Martin
 
RocketJS Nodejs rapid development framework for production web apps
wavome
 
Micro Web Service - Slim and JWT
Tuyen Vuong
 
Caching in asp.net mvc
KarthikaDevi Rajasekaran
 
ITProceed 2015 - Securing Sensitive Data with Azure Key Vault
Tom Kerkhove
 
Ускоряем загрузку картинок вебсокетами
2ГИС Технологии
 
Azure DocumentDB
Shiju Varghese
 
Magento performance feat. core Hacks
Daniel Niedergesäß
 
What's New in Nuxeo Platform 7.3
Nuxeo
 
Azure key vault - Brisbane User Group
Rahul Nath
 
Péhápkaři v Pecce: Jak na bezpečnostní hlavičky – Marek Humpolík – 23. 1. 2019
PeckaDesign.cz
 

Similar to Advance java session 7 (20)

PPTX
Cookie testing
BugRaptors
 
PPTX
Session tracking in servlets
vishal choudhary
 
PDF
07 cookies
snopteck
 
PPTX
Using cookies and sessions
Nuha Noor
 
PPTX
Enterprise java unit-2_chapter-2
sandeep54552
 
PPTX
SessionTrackServlets.pptx
Ranjeet Reddy
 
PPTX
Cookies: HTTP state management mechanism
Jivan Nepali
 
PPTX
Servlet session 10
Anuj Singh Rajput
 
PPTX
Session & Cookies
university of education,Lahore
 
PPTX
Session & Cookies
university of education,Lahore
 
PPTX
Secure Code Warrior - Cookies and sessions
Secure Code Warrior
 
PPTX
Cookies and sessions
Sukrit Gupta
 
PPTX
Cookies
vamsi krishna
 
PPSX
Cookies and session
Soham Sengupta
 
PPT
Cookies in servlet
chauhankapil
 
PPTX
Cookies
vamsitricks
 
PPTX
Internet Cookies
anita gouda
 
PPTX
IMPORTANT SESSION TRACKING TECHNIQUES.pptx
yvtinsane
 
PDF
Servlet sessions
vantinhkhuc
 
PPTX
COOKIES.pptx
18csjeyavarthini
 
Cookie testing
BugRaptors
 
Session tracking in servlets
vishal choudhary
 
07 cookies
snopteck
 
Using cookies and sessions
Nuha Noor
 
Enterprise java unit-2_chapter-2
sandeep54552
 
SessionTrackServlets.pptx
Ranjeet Reddy
 
Cookies: HTTP state management mechanism
Jivan Nepali
 
Servlet session 10
Anuj Singh Rajput
 
Secure Code Warrior - Cookies and sessions
Secure Code Warrior
 
Cookies and sessions
Sukrit Gupta
 
Cookies
vamsi krishna
 
Cookies and session
Soham Sengupta
 
Cookies in servlet
chauhankapil
 
Cookies
vamsitricks
 
Internet Cookies
anita gouda
 
IMPORTANT SESSION TRACKING TECHNIQUES.pptx
yvtinsane
 
Servlet sessions
vantinhkhuc
 
COOKIES.pptx
18csjeyavarthini
 
Ad

More from Smita B Kumar (20)

PPTX
Advance java session 20
Smita B Kumar
 
PPTX
Advance java session 19
Smita B Kumar
 
PPTX
Advance java session 18
Smita B Kumar
 
PPTX
Advance java session 17
Smita B Kumar
 
PPTX
Advance java session 16
Smita B Kumar
 
PPTX
Advance java session 15
Smita B Kumar
 
PPTX
Advance java session 14
Smita B Kumar
 
PPTX
Advance java session 13
Smita B Kumar
 
PPTX
Advance java session 12
Smita B Kumar
 
PPTX
Advance java session 11
Smita B Kumar
 
PPTX
Advance java session 10
Smita B Kumar
 
PPTX
Advance java session 9
Smita B Kumar
 
PPTX
Advance java session 8
Smita B Kumar
 
PPTX
Advance java session 6
Smita B Kumar
 
PPTX
Advance java session 5
Smita B Kumar
 
PPTX
Advance java session 4
Smita B Kumar
 
PPTX
Advance java session 3
Smita B Kumar
 
PPTX
Advance java session 2
Smita B Kumar
 
PPTX
JEE session 1
Smita B Kumar
 
PPTX
01 introduction to struts2
Smita B Kumar
 
Advance java session 20
Smita B Kumar
 
Advance java session 19
Smita B Kumar
 
Advance java session 18
Smita B Kumar
 
Advance java session 17
Smita B Kumar
 
Advance java session 16
Smita B Kumar
 
Advance java session 15
Smita B Kumar
 
Advance java session 14
Smita B Kumar
 
Advance java session 13
Smita B Kumar
 
Advance java session 12
Smita B Kumar
 
Advance java session 11
Smita B Kumar
 
Advance java session 10
Smita B Kumar
 
Advance java session 9
Smita B Kumar
 
Advance java session 8
Smita B Kumar
 
Advance java session 6
Smita B Kumar
 
Advance java session 5
Smita B Kumar
 
Advance java session 4
Smita B Kumar
 
Advance java session 3
Smita B Kumar
 
Advance java session 2
Smita B Kumar
 
JEE session 1
Smita B Kumar
 
01 introduction to struts2
Smita B Kumar
 
Ad

Recently uploaded (20)

PDF
NewMind AI - Journal 100 Insights After The 100th Issue
NewMind AI
 
PPTX
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
PDF
CIFDAQ Weekly Market Wrap for 11th July 2025
CIFDAQ
 
PDF
Impact of IEEE Computer Society in Advancing Emerging Technologies including ...
Hironori Washizaki
 
PDF
SFWelly Summer 25 Release Highlights July 2025
Anna Loughnan Colquhoun
 
PPTX
✨Unleashing Collaboration: Salesforce Channels & Community Power in Patna!✨
SanjeetMishra29
 
PDF
NewMind AI Journal - Weekly Chronicles - July'25 Week II
NewMind AI
 
PDF
LLMs.txt: Easily Control How AI Crawls Your Site
Keploy
 
PDF
Blockchain Transactions Explained For Everyone
CIFDAQ
 
PDF
The Builder’s Playbook - 2025 State of AI Report.pdf
jeroen339954
 
PDF
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
PDF
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
PDF
Human-centred design in online workplace learning and relationship to engagem...
Tracy Tang
 
PDF
Presentation - Vibe Coding The Future of Tech
yanuarsinggih1
 
PDF
Transcript: New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
PPTX
Top iOS App Development Company in the USA for Innovative Apps
SynapseIndia
 
PPTX
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
PPTX
OpenID AuthZEN - Analyst Briefing July 2025
David Brossard
 
PDF
Chris Elwell Woburn, MA - Passionate About IT Innovation
Chris Elwell Woburn, MA
 
PDF
Predicting the unpredictable: re-engineering recommendation algorithms for fr...
Speck&Tech
 
NewMind AI - Journal 100 Insights After The 100th Issue
NewMind AI
 
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
CIFDAQ Weekly Market Wrap for 11th July 2025
CIFDAQ
 
Impact of IEEE Computer Society in Advancing Emerging Technologies including ...
Hironori Washizaki
 
SFWelly Summer 25 Release Highlights July 2025
Anna Loughnan Colquhoun
 
✨Unleashing Collaboration: Salesforce Channels & Community Power in Patna!✨
SanjeetMishra29
 
NewMind AI Journal - Weekly Chronicles - July'25 Week II
NewMind AI
 
LLMs.txt: Easily Control How AI Crawls Your Site
Keploy
 
Blockchain Transactions Explained For Everyone
CIFDAQ
 
The Builder’s Playbook - 2025 State of AI Report.pdf
jeroen339954
 
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
Human-centred design in online workplace learning and relationship to engagem...
Tracy Tang
 
Presentation - Vibe Coding The Future of Tech
yanuarsinggih1
 
Transcript: New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
Top iOS App Development Company in the USA for Innovative Apps
SynapseIndia
 
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
OpenID AuthZEN - Analyst Briefing July 2025
David Brossard
 
Chris Elwell Woburn, MA - Passionate About IT Innovation
Chris Elwell Woburn, MA
 
Predicting the unpredictable: re-engineering recommendation algorithms for fr...
Speck&Tech
 

Advance java session 7

  • 2. Agenda • Stateless nature of HTTP Protocol • Explain the need for Session • Understanding the benefits and drawback of cookies • Sending Outgoing cookies • Receiving Incoming cookies • Tracking repeat visitors • Specifying cookie attributes
  • 3. HTTP Protocol • Stateless Protocol - Communication protocol is stateless - Communication consist of independent pairs of request and response - IP and HTTP are stateless protocols • Advantages of stateless protocol - No need to dynamically allocate storage for conversation - No clean up activity if request dies mid way
  • 4. HTTP Protocol • Cookie is a small text file which get stored on the client side • Cookie contains name and value pair • Cookie is stored only for a particular duration after which they are expired • Client returns the same name and value when it connects to the - Same site - Same domains - Depends upon cookie setting
  • 5. HTTP Protocol • Typical Uses of Cookies - Identifying a user during an e-commerce session - Servlets have a higher-level API for this task - Avoiding username and password - Customizing a site - Focusing advertising
  • 6. Problems with Cookies • The problem is privacy, not security. - Servers can remember your previous actions - If you give out personal information, servers can link that information to your previous actions - Servers can share cookie information through use of a cooperating third party like doubleclick.net - Poorly designed sites store sensitive information like credit card numbers directly in cookie - JavaScript bugs let hostile sites steal cookies (old browsers)
  • 7. Problems with Cookies • The problem is privacy, not security. - Moral for servlet authors • If cookies are not critical to your task, avoid servlets that totally fail when cookies are disabled • Don't put sensitive info in cookies
  • 8. Sending a simple Cookie import java.io.IOException; import java.io.PrintWriter; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class ServletDemo extends HttpServlet{ public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException{ response.setContentType("text/html"); PrintWriter pw = response.getWriter(); Cookie cookie = new Cookie(“name",”talentedge”); cookie.setMaxAge(60*60); //1 hour response.addCookie(cookie); pw.println("Cookies created"); } }
  • 9. Reading Cookies public static String getCookieValue(Cookie[] cookies, String cookieName, String defaultValue) { for(int i=0; i<cookies.length; i++) { Cookie cookie = cookies[i]; if (cookieName.equals(cookie.getName())) return(cookie.getValue()); } return(defaultValue); }
  • 12. Summary • Creates a cookie, a small amount of information sent by a servlet to a Web browser, saved by the browser, and later sent back to the server. A cookie's value can uniquely identify a client, so cookies are commonly used for session management. • A cookie has a name, a single value, and optional attributes such as a comment, path and domain qualifiers, a maximum age, and a version number. Some Web browsers have bugs in how they handle the optional attributes, so use them sparingly to improve the interoperability of your servlets.
  • 13. Summary • The servlet sends cookies to the browser by using the HttpServletResponse.addCookie(javax.servlet.http.Cookie) method, which adds fields to HTTP response headers to send cookies to the browser, one at a time. The browser is expected to support 20 cookies for each Web server, 300 cookies total, and may limit cookie size to 4 KB each. • The browser returns cookies to the servlet by adding fields to HTTP request headers. Cookies can be retrieved from a request by using the HttpServletRequest.getCookies() method. Several cookies might have the same name but different path attributes.