Cookies Class in Servlet Explained with Example

In this tutorial, we will learn the session management technique. There are 4 techniques to manage the session in servlet. Here we will discuss the first techniques to manage the session. Cookie class is the first technique to manage the session. Before discussing Cookie class in detail let's first understand about the cookie.

What are Cookies?

Cookies are the small files that are stored in cache memory of your computer browser so that your browser and websites can track your browsing session and save certain useful information, such as account name and password for later retrieval. Cookies data are stored in the client browser. Because a cookie value can uniquely identify a client, cookies are often used for session tracking.
Read: Basics of Session Tracking


The Cookie Class:

The servlet API provides the javax.servlet.http.Cookie class for working with cookies. You create a cookie with the cookie() constructor. A servlet can write the cookie to a user's machine via the addCookie() method of the HttpServlet Response interface.
Cookies Class in Servlet
The data for that cookies are then included in the header of the HTTP response that is sent to the browser. The name and the values of cookie are stored on the client browser. The information that is saved for each cookie includes the following.

1.The name of the cookie
2.The value of the cookie
3.The expiration date of the cookie
4.The domain and path of the cookie


Methods of the cookie class:

1) public void addCookie(cookie ck) :

A servlet can write the cookie to a user's machine via the addCookie() method of the HttpServletResponse interface.

2) public Cookie[] getCookie() :

A servlet retrieves cookies by calling the getCookies() method of HttpServlet Response.

3) public void setMaxAge(int sec):

It sets the maximum age of the cookie to secs. This is the number of the second after which the cookie is deleted.

4) public void setPath(String p): 

It specifies a path for the cookies

5) public void setComment(String c): 

It sets the comment field of the cookie.

6) public void setVersion(int v): 

it sets the cookie protocol version to v which will be 0 or 1.

7) public String getName(): 

It returns the name of the cookie.

8) public String getPath(): 

it returns the path of the cookie.

9) public int getMaxAge(): 

It returns the age(in seconds).

10) public int getVersion():

It returns the cookie protocol version(will be o or 1)

Example of Cookie in Servlet:

In this example, we are storing the username and password in the cookie object and accessing it in another servlet.


Index.html file:
Cookies Class in Servlet


FirstServlet.java file:
Cookies Class in Servlet


SecondServlet.java file:
Cookies Class in Servlet Explained with Example


web.xml file:
Cookies Class in Servlet Explained with Example


Cookies Class in Servlet Explained with Example


Cookies Class in Servlet Explained with Example


Cookies Class in Servlet Explained with Example


You may also like:

Cookies Class in Servlet Explained with Example Cookies Class in Servlet Explained with Example Reviewed by Prashant Srivastava on October 14, 2018 Rating: 5

No comments:

Powered by Blogger.