<!-- Cookie1.jsp -->
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%><!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)
{
//遍历Cookie
for(Cookie cookie: cookies)
{
out.print(cookie.getName() + ": "+ cookie.getValue());
out.print("<br>");
}
}else{
out.print("没有cookie 存在, 正在创建并返回");
//1. 创建一个Cookie 对象
Cookie cookie = new Cookie("name","wzx");
//设置cookie 的最大时效, 以秒为单位, 若为1, 表示立即删除该 Cookie
//若为负数, 表示不存储Cookie, 若为正数, 表示该Cookie 的存储时间
cookie.setMaxAge(20);
//2. 调用response 的一个方法吧Cookie 传给客户端
response.addCookie(cookie);
}
%>
</body>
</html>