Servlet Cookie Example

In this tutorial you will learn about the Servlet Cookie Example and its application with practical example.

Servlet Cookie Example

In previous article we have learned about cookies, it’s methods and constructor. In this article we are going to implement this thing in our browser.

Let’s understand it by login logout example, we have to create some class. Follow the below steps.


First we need to create index.html file. Check below code.


Lets create login.html file which we are going to use and put the following code in it.

From this Servlet java class we are going to add cookies to our web browser.

Recommended:-  Servlet GenericServlet : This is the profile servlet class from which we will get the cookie. Check below code.

Recommended:-  Servlet ServletConfig : This servlet is used for logout the session and cookie’s age will be replaced by 0. Have a look on method for age of cookies which are discussed in previous article of cookies.

Recommended:-  Servlet ServletRequest



Lets run this example You will get output like below images.

Screen 1:-

This will be our index.html file output. If we click on profile this will give output like second image.


Screen 2:-

This will come when we directly click on Profile button.


Screen 3:-

This will come on when successfully logged in . now when we click on Profile button, we will get our cookies result. Check next screenshot.



Screen 4:-

When we have data in cookies. So this will come.


Screen 5:-

on Successfully logout the below image will appear.


In this tutorial we have learn about the Servlet Cookie Example and its application with practical example. I hope you will like this tutorial.