package com.demo;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.WebServlet;

@WebServlet("/GetCookie")
public class GetCookie extends HttpServlet {
    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException {

        response.setContentType("text/html");
        PrintWriter out = response.getWriter();

        out.println("<html><title>read cookies</title>");
        out.println("<body><h2>Cookies received from browser:</h2>");

        // 获取客户端发送的所有Cookie
        Cookie[] cookies = request.getCookies();
        if (cookies != null) {
            for (Cookie cookie : cookies) {
                out.println("Name: " + cookie.getName() + ", Value: " + cookie.getValue() + "<br>");
            }
        } else {
            out.println("No cookies found.");
        }

        out.println("</body></html>");
    }
}