6/13/22, 2:58 PM Cookies in Servlet - javatpoint
Home Core Java Servlet JSP Struts2 Mail API Hibernate Spring Android
⇧ SCROLL TO TOP
[Link] 1/12
6/13/22, 2:58 PM Cookies in Servlet - javatpoint
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.
Types of Cookie
There are 2 types of cookies in servlets.
1. Non-persistent cookie
2. Persistent cookie
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
⇧ SCROLL TO TOP
[Link] 2/12
6/13/22, 2:58 PM Cookies in Servlet - javatpoint
1. Simplest technique of maintaining the state.
2. Cookies are maintained at client side.
Disadvantage of Cookies
1. It will not work if cookie is disabled from the browser.
2. Only textual information can be set in Cookie object.
Note: Gmail uses cookie technique for login. If you disable the cookie, gmail won't work.
Cookie class
[Link] class provides the functionality of using cookies. It provides a lot of
useful methods for cookies.
Constructor of Cookie class
Constructor Description
Cookie() constructs a cookie.
Cookie(String name, String value) constructs a cookie with a specified name and value.
Useful Methods of Cookie class
There are given some commonly used methods of the Cookie class.
Method Description
public void setMaxAge(int Sets the maximum age of the cookie in seconds.
expiry)
public String getName() Returns the name of the cookie. The name cannot be changed
after creation.
public String getValue() Returns the value of the cookie.
public void setName(String changes the name of the cookie.
⇧ SCROLL TO TOP
name)
[Link] 3/12
6/13/22, 2:58 PM Cookies in Servlet - javatpoint
public void setValue(String changes the value of the cookie.
value)
Other methods required for using Cookies
For adding cookie or getting the value from the cookie, we need some methods provided by
other interfaces. They are:
1. public void addCookie(Cookie ck):method of HttpServletResponse interface is used to
add cookie in response object.
2. public Cookie[] getCookies():method of HttpServletRequest interface is used to return
all the cookies from the browser.
How to create Cookie?
Let's see the simple code to create cookie.
Cookie ck=new Cookie("user","sonoo jaiswal");//creating cookie object
[Link](ck);//adding cookie in the response
How to delete Cookie?
Let's see the simple code to delete cookie. It is mainly used to logout or signout the user.
Cookie ck=new Cookie("user","");//deleting value of cookie
[Link](0);//changing the maximum age to 0 seconds
[Link](ck);//adding cookie in the response
How to get Cookies?
Let's see the simple code to get all the cookies.
Cookie ck[]=[Link]();
for(int i=0;i<[Link];i++){
[Link]("<br>"+ck[i].getName()+" "+ck[i].getValue());//printing name and value of cookie
⇧ SCROLL TO TOP
[Link] 4/12
6/13/22, 2:58 PM Cookies in Servlet - javatpoint
}
Simple example of Servlet Cookies
In this example, we are storing the name of the user in the cookie object and accessing it in
another servlet. As we know well that session corresponds to the particular user. So if you
access it from too many browsers with different values, you will get the different value.
[Link]
<form action="servlet1" method="post">
Name:<input type="text" name="userName"/><br/>
<input type="submit" value="go"/>
</form>
[Link]
import [Link].*;
import [Link].*;
import [Link].*;
public class FirstServlet extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response){
⇧ SCROLL TO TOP
try{
[Link] 5/12
6/13/22, 2:58 PM Cookies in Servlet - javatpoint
[Link]("text/html");
PrintWriter out = [Link]();
String n=[Link]("userName");
[Link]("Welcome "+n);
Cookie ck=new Cookie("uname",n);//creating cookie object
[Link](ck);//adding cookie in the response
//creating submit button
[Link]("<form action='servlet2'>");
[Link]("<input type='submit' value='go'>");
[Link]("</form>");
[Link]();
}catch(Exception e){[Link](e);}
}
}
[Link]
import [Link].*;
import [Link].*;
import [Link].*;
public class SecondServlet extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response){
try{
[Link]("text/html");
PrintWriter out = [Link]();
⇧ SCROLL TO TOP
Cookie ck[]=[Link]();
[Link] 6/12
6/13/22, 2:58 PM Cookies in Servlet - javatpoint
[Link]("Hello "+ck[0].getValue());
[Link]();
}catch(Exception e){[Link](e);}
}
}
[Link]
<web-app>
<servlet>
<servlet-name>s1</servlet-name>
<servlet-class>FirstServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>s1</servlet-name>
<url-pattern>/servlet1</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>s2</servlet-name>
<servlet-class>SecondServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>s2</servlet-name>
<url-pattern>/servlet2</url-pattern>
</servlet-mapping>
</web-app>
⇧ SCROLL TO TOP
[Link] 7/12
6/13/22, 2:58 PM Cookies in Servlet - javatpoint
download this example (developed using Myeclipse IDE)
download this example (developed using Eclipse IDE)
download this example (developed using Netbeans IDE)
Output
⇧ SCROLL TO TOP
[Link] 8/12
6/13/22, 2:58 PM Cookies in Servlet - javatpoint
← Prev
Next →
For Videos Join Our Youtube Channel: Join Now
Feedback
Send your Feedback to feedback@[Link]
Help Others, Please Share
Learn Latest Tutorials
Splunk tutorial SPSS tutorial Swagger T-SQL tutorial
tutorial
Splunk SPSS Transact-SQL
Swagger
⇧ SCROLL TO TOP
[Link] 9/12
6/13/22, 2:58 PM Cookies in Servlet - javatpoint
Tumblr tutorial React tutorial Regex tutorial Reinforcement
learning tutorial
Tumblr ReactJS Regex
Reinforcement
Learning
R Programming RxJS tutorial React Native Python Design
tutorial tutorial Patterns
RxJS
R Programming React Native Python Design
Patterns
Python Pillow Python Turtle Keras tutorial
tutorial tutorial
Keras
Python Pillow Python Turtle
Preparation
Aptitude Logical Verbal Ability Interview
Reasoning Questions
Aptitude Verbal Ability
Reasoning Interview Questions
Company
Interview
Questions
Company Questions
Trending Technologies
Artificial AWS Tutorial Selenium Cloud
Intelligence tutorial Computing
AWS
Tutorial tutorial
Selenium
Artificial Cloud Computing
Intelligence
⇧ SCROLL TO TOP
Hadoop tutorial
[Link] 10/12
6/13/22, 2:58 PM Cookies in Servlet - javatpoint
Hadoop
ReactJS Data Science Angular 7
Tutorial Tutorial Tutorial
ReactJS Data Science Angular 7
Blockchain Git Tutorial Machine DevOps
Tutorial Learning Tutorial Tutorial
Git
Blockchain Machine Learning DevOps
[Link] / MCA
DBMS tutorial Data Structures DAA tutorial Operating
tutorial System tutorial
DBMS DAA
Data Structures Operating System
Computer Compiler Computer Discrete
Network tutorial Design tutorial Organization and Mathematics
Architecture Tutorial
Computer Network Compiler Design
Computer Discrete
Organization Mathematics
Ethical Hacking Computer Software html tutorial
Tutorial Graphics Tutorial Engineering
Web Technology
Tutorial
Ethical Hacking Computer Graphics
Software
Engineering
Cyber Security Automata C Language C++ tutorial
tutorial Tutorial tutorial
C++
Cyber Security Automata C Programming
Java tutorial .Net Python tutorial List of
Framework Programs
Java Python
tutorial
Programs
.Net
⇧ SCROLL TO TOP
[Link] 11/12
6/13/22, 2:58 PM Cookies in Servlet - javatpoint
Control Data Mining Data
Systems tutorial Tutorial Warehouse
Tutorial
Control System Data Mining
Data Warehouse
⇧ SCROLL TO TOP
[Link] 12/12