码迷,mamicode.com
首页 > 其他好文 > 详细

Cookie管理

时间:2015-08-27 15:00:12      阅读:150      评论:0      收藏:0      [点我收藏+]

标签:

1,判断来访者是否第一次

public class VistorTest extends HttpServlet {

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
resp.setContentType("text/html");
String msg = null;
boolean isNew = true;
PrintWriter out = resp.getWriter();
Cookie[] cookies = req.getCookies();
if(cookies!=null){
for(int i=0;i<cookies.length;i++){
Cookie cookie = cookies[i];
if(cookie.getName().equals("repeatVistor")&&
cookie.getValue().equals("yes")){
isNew=false;
break;
}
}
}

if(isNew){
Cookie c = new Cookie("repeatVistor", "yes");
c.setMaxAge(365*24*60*60);
resp.addCookie(c);
msg = "welcome aboard";
}
else
msg="welcome back";

out.write(msg);

}

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
super.doGet(req, resp);
}

 

}

 

会话Cookie与持续性cookie(区别就是是否设置了setMaxAge)

 

Cookie管理

标签:

原文地址:http://www.cnblogs.com/daxiong225/p/4763054.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!