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

PDF
AtlasCamp 2014: 10 Things a Front End Developer Should Know About Connect
PPTX
Advance Java
PPTX
Techdays Finland 2018 - Building secure cloud applications with Azure Key Vault
PPTX
How it's made - MyGet.org - AzureConf
PPTX
Securing MongoDB to Serve an AWS-Based, Multi-Tenant, Security-Fanatic SaaS A...
PPTX
AZUG.BE - Azure User Group Belgium - First public meeting
PPTX
Node.js Blockchain Implementation
PPTX
How to use windows azure features on windows
AtlasCamp 2014: 10 Things a Front End Developer Should Know About Connect
Advance Java
Techdays Finland 2018 - Building secure cloud applications with Azure Key Vault
How it's made - MyGet.org - AzureConf
Securing MongoDB to Serve an AWS-Based, Multi-Tenant, Security-Fanatic SaaS A...
AZUG.BE - Azure User Group Belgium - First public meeting
Node.js Blockchain Implementation
How to use windows azure features on windows

What's hot (20)

PPTX
Sqlviking
PDF
[Cluj] CSP (Content Security Policy)
PPT
jclouds overview
PPT
Node js
PDF
Is OSGi Modularity Always Worth It? - Glyn Normington
PPTX
Java Session
PPTX
Azure Low Lands 2019 - Building secure cloud applications with Azure Key Vault
PDF
https://docs.google.com/presentation/d/1DcL4zK6i3HZRDD4xTGX1VpSOwyu2xBeWLT6a_...
PPTX
The Azure API-ness Factory
PPTX
Protecting your online and on premises assets “Cloud Style”
KEY
RocketJS Nodejs rapid development framework for production web apps
PPTX
Micro Web Service - Slim and JWT
PPTX
Caching in asp.net mvc
PPTX
ITProceed 2015 - Securing Sensitive Data with Azure Key Vault
PDF
Ускоряем загрузку картинок вебсокетами
PPSX
Azure DocumentDB
PPTX
Magento performance feat. core Hacks
PDF
What's New in Nuxeo Platform 7.3
PPTX
Azure key vault - Brisbane User Group
PDF
Péhápkaři v Pecce: Jak na bezpečnostní hlavičky – Marek Humpolík – 23. 1. 2019
Sqlviking
[Cluj] CSP (Content Security Policy)
jclouds overview
Node js
Is OSGi Modularity Always Worth It? - Glyn Normington
Java Session
Azure Low Lands 2019 - Building secure cloud applications with Azure Key Vault
https://docs.google.com/presentation/d/1DcL4zK6i3HZRDD4xTGX1VpSOwyu2xBeWLT6a_...
The Azure API-ness Factory
Protecting your online and on premises assets “Cloud Style”
RocketJS Nodejs rapid development framework for production web apps
Micro Web Service - Slim and JWT
Caching in asp.net mvc
ITProceed 2015 - Securing Sensitive Data with Azure Key Vault
Ускоряем загрузку картинок вебсокетами
Azure DocumentDB
Magento performance feat. core Hacks
What's New in Nuxeo Platform 7.3
Azure key vault - Brisbane User Group
Péhápkaři v Pecce: Jak na bezpečnostní hlavičky – Marek Humpolík – 23. 1. 2019
Ad

Similar to Advance java session 7 (20)

PPTX
Cookie testing
PPTX
Session tracking in servlets
PDF
07 cookies
PPTX
Using cookies and sessions
PPTX
Enterprise java unit-2_chapter-2
PPTX
SessionTrackServlets.pptx
PPTX
Cookies: HTTP state management mechanism
PPTX
Servlet session 10
PPTX
PPTX
PPTX
Secure Code Warrior - Cookies and sessions
PPTX
Cookies and sessions
PPTX
Cookies
PPSX
Cookies and session
PPT
Cookies in servlet
PPTX
Cookies
PPTX
Internet Cookies
PPTX
IMPORTANT SESSION TRACKING TECHNIQUES.pptx
PDF
Servlet sessions
PPTX
COOKIES.pptx
Cookie testing
Session tracking in servlets
07 cookies
Using cookies and sessions
Enterprise java unit-2_chapter-2
SessionTrackServlets.pptx
Cookies: HTTP state management mechanism
Servlet session 10
Secure Code Warrior - Cookies and sessions
Cookies and sessions
Cookies
Cookies and session
Cookies in servlet
Cookies
Internet Cookies
IMPORTANT SESSION TRACKING TECHNIQUES.pptx
Servlet sessions
COOKIES.pptx
Ad

More from Smita B Kumar (20)

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

Recently uploaded (20)

PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Machine learning based COVID-19 study performance prediction
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Spectral efficient network and resource selection model in 5G networks
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
20250228 LYD VKU AI Blended-Learning.pptx
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
The AUB Centre for AI in Media Proposal.docx
Review of recent advances in non-invasive hemoglobin estimation
Diabetes mellitus diagnosis method based random forest with bat algorithm
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Reach Out and Touch Someone: Haptics and Empathic Computing
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Building Integrated photovoltaic BIPV_UPV.pdf
MYSQL Presentation for SQL database connectivity
Mobile App Security Testing_ A Comprehensive Guide.pdf
Machine learning based COVID-19 study performance prediction
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
NewMind AI Weekly Chronicles - August'25 Week I
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Spectral efficient network and resource selection model in 5G networks
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Unlocking AI with Model Context Protocol (MCP)
Build a system with the filesystem maintained by OSTree @ COSCUP 2025

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.