<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" session="false"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
//1.获取cookie
Cookie[] cookies = request.getCookies();
if(cookies != null && cookies.length > 0){
for(Cookie cookie : cookies){
//2.获取cookie的name和value
out.print(cookie.getName() + ":" + cookie.getValue());
out.print("<br>");
}
}else{
//3.创建一个cookie对象
out.print("没有一个cookie,正在创建并返回");
Cookie cookie = new Cookie("ztt","123");
//setMaxAge:设置Cookie的最大时效,以秒为单位,若为0,表示立即删除该cookie
cookie.setMaxAge(30);
//调用response的一个方法把cookie传给客户端
response.addCookie(cookie);
}
%>
</body>
</html>