Cookies 我可以向会话cookie添加最长到期日期吗?
我已经设置了一个,它没有任何到期日期,因此在浏览器关闭时将被删除 现在我想添加一个最长的到期日,意思是Cookies 我可以向会话cookie添加最长到期日期吗?,cookies,Cookies,我已经设置了一个,它没有任何到期日期,因此在浏览器关闭时将被删除 现在我想添加一个最长的到期日,意思是 如果浏览器在最长到期日之前关闭,则“我的cookie”将被删除 否则,我的cookie将在超过最长到期日期后被删除 请注意,我不想设置“常规”到期日,因为这会使我的cookie在到期日之前关闭浏览器时无法删除 我找到的唯一解决方案是拥有第二个持久cookie,最长有效期:如果第二个cookie未找到(过期),我手动删除第一个cookie。因为我想在cookies中写尽可能少的信息,所以我更
- 如果浏览器在最长到期日之前关闭,则“我的cookie”将被删除
- 否则,我的cookie将在超过最长到期日期后被删除
Cookie::setMaxAge(int)
在Java中,我们必须指定过期时间,您可以使用javax.servlet.http.Cookie的方法。
它将Cookie过期前的秒数作为参数
例如,对于5分钟的到期时间,我们将执行以下操作:-
// Create a new cookie for userID from a fictitious
// method called getUserID
Cookie cookie = new Cookie ("userID", getUserID());
// Expire the cookie in five minutes (5 * 60)
cookie.setMaxAge( 300 );
当使用HttpServletResponse.addCookie(cookie)
将cookie发送回浏览器时,浏览器将只返回cookie,直到过期日期。如果愿意,还可以为setMaxAge(int)
指定负值,cookie将在浏览器退出后立即过期。但是请注意,并不是每个人都会关闭他们的浏览器,它可能会在几分钟、几小时甚至几天内可用。最后,指定值为零将使cookie立即过期
下面是完整的Javaservlet示例
// Import required java libraries
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
// Extend HttpServlet class
public class HelloForm extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// Create cookies for first and last names.
Cookie firstName = new Cookie("first_name", request.getParameter("first_name"));
Cookie lastName = new Cookie("last_name", request.getParameter("last_name"));
// Set expiry date after 24 Hrs for both the cookies.
firstName.setMaxAge(60*60*24);
lastName.setMaxAge(60*60*24);
// Add both the cookies in the response header.
response.addCookie( firstName );
response.addCookie( lastName );
// Set response content type
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String title = "Setting Cookies Example";
String docType =
"<!doctype html public \"-//w3c//dtd html 4.0 " + "transitional//en\">\n";
out.println(docType +
"<html>\n" +
"<head>
<title>" + title + "</title>
</head>\n" +
"<body bgcolor = \"#f0f0f0\">\n" +
"<h1 align = \"center\">" + title + "</h1>\n" +
"<ul>\n" +
" <li><b>First Name</b>: "
+ request.getParameter("first_name") + "\n" +
" <li><b>Last Name</b>: "
+ request.getParameter("last_name") + "\n" +
"</ul>\n" +
"</body>
</html>"
);
}
}
//导入所需的java库
导入java.io.*;
导入javax.servlet.*;
导入javax.servlet.http.*;
//扩展HttpServlet类
公共类HelloForm扩展了HttpServlet{
public void doGet(HttpServletRequest请求、HttpServletResponse响应)
抛出ServletException、IOException{
//为名字和姓氏创建cookie。
Cookie firstName=新Cookie(“first_name”,request.getParameter(“first_name”);
Cookie lastName=新Cookie(“last_name”,request.getParameter(“last_name”);
//为两个cookie设置24小时后的到期日期。
firstName.setMaxAge(60*60*24);
lastName.setMaxAge(60*60*24);
//在响应头中添加这两个cookie。
addCookie(名字);
addCookie(lastName);
//设置响应内容类型
response.setContentType(“text/html”);
PrintWriter out=response.getWriter();
String title=“设置Cookies示例”;
字符串docType=
“\n”;
out.println(docType+
“\n”+
"
“+标题+”
\n“+
“\n”+
“”+标题+“\n”+
“\n”+
“- 名字:”
+request.getParameter(“名字”)+“\n”+
“
- 姓氏:”
+request.getParameter(“姓氏”)+“\n”+
“
\n”+
"
"
);
}
}
和HtML文件将被删除
<html>
<body>
<form action = "HelloForm" method = "GET">
First Name: <input type = "text" name = "first_name">
<br />
Last Name: <input type = "text" name = "last_name" />
<input type = "submit" value = "Submit" />
</form>
</body>
</html>
名字:
姓氏:
如果要将cookie保留为会话cookie,则不能设置到期时间。
所以您可以将时间戳设置为cookie值,或者创建新cookie并将值设置为时间戳
var timestamp = (new Date()).getTime()
document.cookie = "cookiename=value; path=/";
document.cookie = "expirycookie="+timestamp+"; path=/";
对于唯一的客户端解决方案,您可以设置检查cookie时间戳值的间隔。
将下面的代码添加到所有页面
var interval = setInterval(function(){
var timeStamp = getCookie('expirycookie')
if(!timeStamp){clearInterval(interval); return}
var cookieDuration = 5*60*1000 //expire cookie after 5 min
if(timeStamp < (new Date()).getTime() - cookieDuration){
//cookie expired delete here
document.cookie = 'cookiename=value; Path=/; Expires=Thu, 01 Jan 1970 00:00:01 GMT;';
document.cookie = 'expirycookie=value; Path=/; Expires=Thu, 01 Jan 1970 00:00:01 GMT;';
clearInterval(interval)
}
},1000)
function getCookie(cname) {
var name = cname + "=";
var ca = document.cookie.split(';');
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') c = c.substring(1);
if (c.indexOf(name) != -1) return c.substring(name.length, c.length);
}
return "";
}
var interval=setInterval(函数(){
var timeStamp=getCookie('expirycookie')
if(!timeStamp){clearInterval(interval);return}
var cookieDuration=5*60*1000//在5分钟后使cookie过期
if(时间戳<(新日期()).getTime()-cookieDuration){
//cookie过期,请在此处删除
document.cookie='cookiename=value;Path=/;Expires=Thu,1970年1月1日00:00:01 GMT;';
document.cookie='expireycookie=value;Path=/;Expires=Thu,1970年1月1日00:00:01 GMT;';
清除间隔(间隔)
}
},1000)
函数getCookie(cname){
变量名称=cname+“=”;
var ca=document.cookie.split(“;”);
对于(变量i=0;i
通过这一点,您到底想要实现什么?(我假设您不仅设置了客户端意义上的“会话cookie”,还设置了与之关联的服务器端会话?在这种情况下,您应该将上次访问时间戳存储到会话中,然后在下次请求时检查时差是否过大。)@CBroe我进行web分析。我没有服务器端会话。我确实有一个客户端意义上的会话cookie,我正在根据该cookie按访问对页面视图进行分组,并希望有一个最大访问持续时间,这是您在处理数据时应该处理的事情。//特别是分析型的