package com.test;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
-
Servlet implementation class cookie
*/
public class cookie extends HttpServlet {protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Cookie[] cookies=request.getCookies();
if(cookies != null) {
for (Cookie cookie : cookies) {
System.out.println(cookie.getName()+"---"+cookie.getValue());
}
}
Cookie cookie = new Cookie("name","zhangsan");
response.addCookie(cookie);
Cookie cookie2 = new Cookie("age","18");
response.addCookie(cookie2);
//设置cookie有效期,参数以秒计算
cookie.setMaxAge(600);response.getWriter().write(cookie.getName()+"---"+cookie.getValue()+"---"+cookie2.getName()+"---"+cookie2.getValue());
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}