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.

index.html

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

login.html

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

LoginServlet.java

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

Recommended:-  Servlet GenericServlet

ProfileServlet.java : This is the profile servlet class from which we will get the cookie. Check below code.

Recommended:-  Servlet ServletConfig

LogoutServlet.java : 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

 

Output:-

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.

image002

Screen 2:-

This will come when we directly click on Profile button.

image004

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.

image005

 

Screen 4:-

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

image007

Screen 5:-

on Successfully logout the below image will appear.

image009

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