How to Read Cookies in Servlet

Read Cookies in Servlet

In this example, we will show you simple program about how to read cookies in servlet. The example program has been tested with Google Chrome browser and output shared in the below post.

Note: Before going to proceed with this example make sure that, you have already created cookies using servlet. We assume that you have created cookies and Its available in the browser already.

Servlet Class (

package com.dineshkrish.servlet;


import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

 * @author Dinesh Krishnan

public class MyCookieServlet extends HttpServlet {

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

		PrintWriter out = response.getWriter();


		// getting all cookies as array from request object
		Cookie[] cookies = request.getCookies();

		// iterating cookies array
		if (cookies != null) {

			for (int i = 0; i < cookies.length; i++) {

				out.println("<li>"+cookies[i].getName()+" - "+cookies[i].getValue()+"</li>");



Configuration File (web.xml)

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi=""
	id="WebApp_ID" version="3.0">





Read Cookies in Servlet


1. How to Create Cookies in Servlet
2. Java EE HttpServletRequest Interface
3. Java EE HttpServletResponse Interface
4. Java EE ServletException Class
5. Java IOException Class
6. Java EE Cookie Class

Hello, folks, I am a founder of I love Java and Open source technologies, If you find my tutorials are useful, please consider making donations to these charities.