您的位置:首页 > 编程语言 > Java开发

JavaWeb基础 Cookie maxAge大于0 等于0

2017-12-05 21:27 211 查看
礼悟:
   公恒学思合行悟,尊师重道存感恩。叶见寻根三返一,江河湖海同一体。
虚怀若谷良心主,愿行无悔给最苦。读书锻炼养身心,诚劝且行且珍惜。

javaEE:7
javaSE:1.8
JSTL:1.2.2
server:tomcat 8.5
browser:Chrome
os:windows7 x64
ide:MyEclipse 2017


当maxAge>0时

addCookie.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'addCookie.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->

</head>

<body>
<h2>客户端进行保存cookie</h2>
<%
// cookie就是一个请求头

// 创建cookie
Cookie cookie = new Cookie("name","cnblogs-jizuiku");

cookie.setMaxAge(60*60);
// 添加cookie
response.addCookie(cookie);

%>
</body>
</html>


浏览器访问指定页面



查看cookie



当maxAge=0时

addCookie.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'addCookie.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->

</head>

<body>
<h2>客户端进行保存cookie</h2>
<%
// cookie就是一个请求头

// 创建cookie
Cookie cookie = new Cookie("name","cnblogs-jizuiku");

cookie.setMaxAge(60*60);
// 添加cookie
response.addCookie(cookie);

%>
</body>
</html>


delCookie.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'addCookie.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->

</head>

<body>
<h2>删除名字为name的cookie</h2>
<%
// cookie就是一个请求头

// 创建cookie
Cookie cookie = new Cookie("name","");
// 将maxAge=0
// 客户端访问后,名字是name的cookie的将会被删除
cookie.setMaxAge(0);
// 添加cookie
response.addCookie(cookie);

%>
</body>
</html>


浏览器访问 addCookie.jsp 并 查看cookie





浏览器访问 delCookie.jsp 并查看cookie



扩展(来自视频教程中的截图)

  


学习资源:itcast和itheima视频库。如果您有公开的资源,可以分享给我的话,用您的资源学习也可以。
博文是观看视频后,融入思考写成的。博文好,是老师讲得好。博文坏,是 给最苦 没认真。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: