package org.bysocket.http;
import java.io.IOException;
import java.io.PrintWriter;
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(urlPatterns="/COOKIE")
public class COOKIEServletT extends HttpServlet
{
private static final long serialVersiOnUID= 1L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
// 获取COOKIE
COOKIE[] COOKIEs = req.getCOOKIEs();
for (COOKIE COOKIE : COOKIEs)
System.out.println(COOKIE.getName() + " " + COOKIE.getValue());
// 创建COOKIE
COOKIE COOKIE = new COOKIE("COOKIEName", "COOKIEValue");
COOKIE.setMaxAge(10);
COOKIE.setHttpOnly(true);
resp.addCOOKIE(COOKIE);
// 响应
PrintWriter pw = resp.getWriter();
pw.print("<
html
><
body
><
h1
>Hello,COOKIE!
h1
>
body
>
html
>");
}
}