How do I read cookie in Servlet?

package org.kodejava.servlet;

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

@WebServlet(name = "ReadCookieServlet", urlPatterns = "/read-cookie")
public class ReadCookieExample extends HttpServlet {

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

    protected void doPost(HttpServletRequest request,
                          HttpServletResponse response) throws IOException {
        PrintWriter writer = response.getWriter();

        Cookie[] cookies = request.getCookies();
        for (Cookie cookie : cookies) {
            writer.println("Name: " + cookie.getName() + "; Value: " + cookie.getValue());

An example result of the servlet above is:

Name: username; Value: jduke 
Name: JSESSIONID; Value: AE819A3F74E3700838ADFC99485216DF

Maven dependencies


Maven Central


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.