SlideShare a Scribd company logo
2
Most read
3
Most read
6
Most read
Cookies in Servlet
Prof. Neeraj Bhargava
Kapil Chauhan
Department of Computer Science
School of Engineering & Systems Sciences
MDS University, Ajmer
Cookies in Servlet
 A cookie is a small piece of information that is
persisted between the multiple client requests.
 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.
How Cookie works
 By default, each request is considered as a new
request.
 In cookies technique, we add cookie with response
from the servlet.
 So cookie is stored in the cache of the browser. After
that if request is sent by the user, cookie is added with
request by default.
 Thus, we recognize the user as the old user.
Cont..
 There are three steps involved in identifying returning
users −
 Server script sends a set of cookies to the browser. For
example name, age, or identification number etc.
 Browser stores this information on local machine for
future use.
 When next time browser sends any request to web
server then it sends those cookies information to the
server and server uses that information to identify the
user.
Types of Cookie
 There are 2 types of cookies in servlets.
 Non-persistent cookie
 Persistent cookie
Cont…
 Non-persistent cookie
 It is valid for single session only. It is removed each
time when user closes the browser.
 Persistent cookie
 It is valid for multiple session . It is not removed
each time when user closes the browser. It is removed
only if user logout or signout.
Advantage of Cookies
 Simplest technique of maintaining the state.
 Cookies are maintained at client side.
Disadvantage of Cookies
 It will not work if cookie is disabled from the browser.
 Only textual information can be set in Cookie object.
Example public class FirstServlet extends HttpServlet {
 public void doPost(HttpServletRequest request, HttpServletResponse response){
 try{
 response.setContentType("text/html");
 PrintWriter out = response.getWriter();

 String n=request.getParameter("userName");
 out.print("Welcome "+n);
 Cookie ck=new Cookie("u",n);
 response.addCookie(ck);

 out.print("<form action='servlet2' method='post'>");
 out.print("<input type='submit' value='go'>");
 out.print("</form>");

 out.close();
 }catch(Exception e){System.out.println(e);}
 }
 }
Assignment
 Explain cookies in java script with suitable example.

More Related Content

PDF
File and directories in python
PDF
INTRODUCTION TO UML DIAGRAMS
PPTX
Packages In Python Tutorial
PPT
Introduction to Data Warehouse
PDF
고려대학교 컴퓨터학과 특강 - 대학생 때 알았더라면 좋았을 것들
PDF
NOSQL- Presentation on NoSQL
PPTX
PPTX
Principles of animation
File and directories in python
INTRODUCTION TO UML DIAGRAMS
Packages In Python Tutorial
Introduction to Data Warehouse
고려대학교 컴퓨터학과 특강 - 대학생 때 알았더라면 좋았을 것들
NOSQL- Presentation on NoSQL
Principles of animation

What's hot (20)

PPTX
Delegates and events in C#
PPTX
Content provider in_android
PPTX
2D viewing & clipping
PPTX
Javascript operators
PPT
SQLITE Android
PPT
Character generation
PPT
android activity
PPTX
Servlets
PPTX
HTML Forms
PPT
2.2 decision tree
PPT
Java awt
PPTX
Android Services
PPTX
Line Drawing Algorithms - Computer Graphics - Notes
PDF
Asp.net state management
PPTX
Session tracking in servlets
PDF
The Object Model
PPT
RichControl in Asp.net
PDF
C++ Files and Streams
PPT
Session Tracking in servlets
PPTX
Php cookies
Delegates and events in C#
Content provider in_android
2D viewing & clipping
Javascript operators
SQLITE Android
Character generation
android activity
Servlets
HTML Forms
2.2 decision tree
Java awt
Android Services
Line Drawing Algorithms - Computer Graphics - Notes
Asp.net state management
Session tracking in servlets
The Object Model
RichControl in Asp.net
C++ Files and Streams
Session Tracking in servlets
Php cookies
Ad

Similar to Cookies in servlet (20)

PPTX
Servlet session 10
PPTX
Enterprise java unit-2_chapter-2
PPTX
Sessions&cookies
PPTX
Authentication in Svelte using cookies.pptx
PPTX
Session tracking In Java
PPTX
Working with in cookies java seminar.pptx
PPTX
Using cookies and sessions
PPTX
SessionTrackServlets.pptx
PPTX
WORKING WITH IN COOKIES JAVA SEMINAR.pptx
PPSX
Sessions and cookies
PPTX
Cookies in servlets.ppt
PPTX
IMPORTANT SESSION TRACKING TECHNIQUES.pptx
PPTX
Cookies-PHP
PPTX
PPTX
PPTX
Enterprise java unit-2_chapter-3
PPTX
Cookies and Session
PDF
Cookies in Angular | Install CookiesService
PPTX
Cookies and sessions
PPT
Servlet session 10
Enterprise java unit-2_chapter-2
Sessions&cookies
Authentication in Svelte using cookies.pptx
Session tracking In Java
Working with in cookies java seminar.pptx
Using cookies and sessions
SessionTrackServlets.pptx
WORKING WITH IN COOKIES JAVA SEMINAR.pptx
Sessions and cookies
Cookies in servlets.ppt
IMPORTANT SESSION TRACKING TECHNIQUES.pptx
Cookies-PHP
Enterprise java unit-2_chapter-3
Cookies and Session
Cookies in Angular | Install CookiesService
Cookies and sessions
Ad

More from chauhankapil (20)

PPTX
Gray level transformation
PPTX
Elements of visual perception
PPTX
JSP Client Request
PPTX
Jsp server response
PPTX
Markov decision process
PPTX
RNN basics in deep learning
PPTX
Introduction to generative adversarial networks (GANs)
PPTX
Bayesian probabilistic interference
PPTX
PPTX
Exception handling in java
PPTX
Knowledge acquistion
PPTX
Knowledge based system
PPTX
Introduction of predicate logics
PPTX
Types of inheritance in java
PPTX
Representation of syntax, semantics and Predicate logics
PPTX
Inheritance in java
PPTX
Propositional logic
PPTX
Constructors in java
PPTX
Methods in java
PPT
Circular linked list
Gray level transformation
Elements of visual perception
JSP Client Request
Jsp server response
Markov decision process
RNN basics in deep learning
Introduction to generative adversarial networks (GANs)
Bayesian probabilistic interference
Exception handling in java
Knowledge acquistion
Knowledge based system
Introduction of predicate logics
Types of inheritance in java
Representation of syntax, semantics and Predicate logics
Inheritance in java
Propositional logic
Constructors in java
Methods in java
Circular linked list

Recently uploaded (20)

PPTX
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
DOCX
573137875-Attendance-Management-System-original
PDF
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
PDF
Digital Logic Computer Design lecture notes
PPTX
Lecture Notes Electrical Wiring System Components
PPTX
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
PDF
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
PPTX
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
PDF
Automation-in-Manufacturing-Chapter-Introduction.pdf
PPT
Project quality management in manufacturing
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PPT
CRASH COURSE IN ALTERNATIVE PLUMBING CLASS
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PPTX
OOP with Java - Java Introduction (Basics)
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PPTX
Sustainable Sites - Green Building Construction
PPTX
Internet of Things (IOT) - A guide to understanding
PPTX
Geodesy 1.pptx...............................................
PPTX
web development for engineering and engineering
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
573137875-Attendance-Management-System-original
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
Digital Logic Computer Design lecture notes
Lecture Notes Electrical Wiring System Components
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
Automation-in-Manufacturing-Chapter-Introduction.pdf
Project quality management in manufacturing
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
CRASH COURSE IN ALTERNATIVE PLUMBING CLASS
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
OOP with Java - Java Introduction (Basics)
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
Sustainable Sites - Green Building Construction
Internet of Things (IOT) - A guide to understanding
Geodesy 1.pptx...............................................
web development for engineering and engineering
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026

Cookies in servlet

  • 1. Cookies in Servlet Prof. Neeraj Bhargava Kapil Chauhan Department of Computer Science School of Engineering & Systems Sciences MDS University, Ajmer
  • 2. Cookies in Servlet  A cookie is a small piece of information that is persisted between the multiple client requests.  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.
  • 3. How Cookie works  By default, each request is considered as a new request.  In cookies technique, we add cookie with response from the servlet.  So cookie is stored in the cache of the browser. After that if request is sent by the user, cookie is added with request by default.  Thus, we recognize the user as the old user.
  • 4. Cont..  There are three steps involved in identifying returning users −  Server script sends a set of cookies to the browser. For example name, age, or identification number etc.  Browser stores this information on local machine for future use.  When next time browser sends any request to web server then it sends those cookies information to the server and server uses that information to identify the user.
  • 5. Types of Cookie  There are 2 types of cookies in servlets.  Non-persistent cookie  Persistent cookie
  • 6. Cont…  Non-persistent cookie  It is valid for single session only. It is removed each time when user closes the browser.  Persistent cookie  It is valid for multiple session . It is not removed each time when user closes the browser. It is removed only if user logout or signout.
  • 7. Advantage of Cookies  Simplest technique of maintaining the state.  Cookies are maintained at client side.
  • 8. Disadvantage of Cookies  It will not work if cookie is disabled from the browser.  Only textual information can be set in Cookie object.
  • 9. Example public class FirstServlet extends HttpServlet {  public void doPost(HttpServletRequest request, HttpServletResponse response){  try{  response.setContentType("text/html");  PrintWriter out = response.getWriter();   String n=request.getParameter("userName");  out.print("Welcome "+n);  Cookie ck=new Cookie("u",n);  response.addCookie(ck);   out.print("<form action='servlet2' method='post'>");  out.print("<input type='submit' value='go'>");  out.print("</form>");   out.close();  }catch(Exception e){System.out.println(e);}  }  }
  • 10. Assignment  Explain cookies in java script with suitable example.