package com.demo;

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

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

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

        // 创建一个Cookie对象
        Cookie userCookie = new Cookie("user", "uid1234");
        // 设置Cookie的有效期为7天
        userCookie.setMaxAge(60 * 60 * 24 * 7);
        // 将Cookie添加到响应中
        response.addCookie(userCookie);

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

        out.println("<html><title>add cookies</title>");
        out.println("<body><h2>A cookie has been sent to browser</h2></body>");
        out.println("</html>");
    }
}