1. Create Login.html
  2. Create a CookieServlet1 class.
  3. Create a CookieServlet2 class.
  4. Configure CookieServlet1 & CookieServlet2 classes in web.xml
  5. Deploy & Run the application

Create Login.html

<html>
<head>
<title>Form</title>
</head>
<body>
	<form action="/SessionDemo/test1">
		<h2>Enter Session Information</h2>
		Name:<input type="text" name="uname"><br> Value:<input
			type="text" name="uvalue"><br> <input type="submit">
	</form>
	<a href="/SessionDemo/test2">View Cookies Information</a>
</body>
</html>

Create a CookieServlet1 class.

package com.java2learn.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class CookieServlet1 extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		
		String name = request.getParameter("uname");
		String value = request.getParameter("uvalue");
		
		Cookie c=new Cookie(name, value);
		c.setMaxAge(120);
		response.addCookie(c);
		out.println("<h1>Cookie Added Succesfully.</h1>");
		
		RequestDispatcher rd = request.getRequestDispatcher("Login.html");
		rd.include(request, response);
		
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

}

Create a CookieServlet2 class.

package com.java2learn.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.Enumeration;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class CookieServlet2 extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		PrintWriter out = response.getWriter();
		Cookie[] c=request.getCookies();
		if(c==null){
			out.println("No Cookie associated with request");
		}else{
			for(Cookie c1:c){
				out.println(c1.getName()+"---"+c1.getValue());
			}
		}
		
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

}

Configure CookieServlet1 & CookieServlet2 classes in web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	id="WebApp_ID" version="2.5">
	<display-name>SessionDemo</display-name>
	
	<servlet>
		<description></description>
		<display-name>CookieServlet1</display-name>
		<servlet-name>CookieServlet1</servlet-name>
		<servlet-class>com.java2learn.servlet.CookieServlet1</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>CookieServlet1</servlet-name>
		<url-pattern>/test1</url-pattern>
	</servlet-mapping>
	<servlet>
		<description></description>
		<display-name>CookieServlet2</display-name>
		<servlet-name>CookieServlet2</servlet-name>
		<servlet-class>com.java2learn.servlet.CookieServlet2</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>CookieServlet2</servlet-name>
		<url-pattern>/test2</url-pattern>
	</servlet-mapping>
</web-app>

Final Package Structure
Packagestructure

Deploy & Run the Application.

Input Page
Input
Enter Inputs:
EnterInputs
after clicking on submit button
submitbuttion
Click on View Cookies Information
ClickonViewCookiesLink

Download Project:
SessionDemo