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)

设置时间1小时

public class PesistAndTemp extends HttpServlet {

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
resp.setContentType("text/html");
PrintWriter out = resp.getWriter();
for(int i=0;i<3;i++){
Cookie cookie=new Cookie("temp-cookie"+i, "cookie-value"+i);
resp.addCookie(cookie);
cookie = new Cookie("pesist-cookie-"+i, "pesis-vlaue"+i);
cookie.setMaxAge(24*60*60);
resp.addCookie(cookie);
}
out.write("<p>active cookies</p>");
Cookie[] cookies=req.getCookies();
if(cookies==null){
out.write("<p>NO MATCHES</p>");
}
else{
for(int i=0;i<cookies.length;i++){
Cookie c = cookies[i];
out.write("<p>cookieName:"+c.getName()+" cookie-value:"+c.getValue());
}
}

}

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

}

第一次访问

在同一会话中1小时内访问

首次访问一小时内另一个会话再次访问:

3。修改Cookie值:记录用户的访问计数:

public class CountVistor extends HttpServlet {

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
resp.setContentType("text/html");
PrintWriter out = resp.getWriter();
boolean isFirst=true;
int count=1;
Cookie c;
Cookie[] cookies = req.getCookies();
if(cookies!=null){

for(int i=0;i<cookies.length;i++){
c = cookies[i];
if(c.getName().equals("accesscount")){
c = new Cookie("accesscount", String.valueOf(Integer.parseInt(c.getValue())+1));
c.setMaxAge(60*60);
resp.addCookie(c);
count =Integer.parseInt( c.getValue());
System.out.println("not first");
isFirst=false;
}
}

}

else if(isFirst){
c = new Cookie("accesscount", String.valueOf(count));
c.setMaxAge(60*60);
resp.addCookie(c);
System.out.println("first");
}
out.write("this is you "+count+" time vistor");

}

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

}

输出:this is you 36 time vistor

Cookie管理的更多相关文章

  1. JMeter中HTTP Cookie 管理器使用

    案例: 在一次做公司OA系统的时候,发现录制脚本无法回放成功,通过定位,是因为登录的过程中存在重定向,导致登录接口的状态没有自动带入重定向页面 解决方法: 加入HTTP Cookie 管理器使用 现象 ...

  2. nodejs cookie管理

    Cookie 管理 我们可以使用中间件向 Node.js 服务器发送 cookie 信息,以下代码输出了客户端发送的 cookie 信息: // express_cookie.js 文件 var ex ...

  3. JMeter HTTP Cookie管理器的跨域使用

    Jmeter的一个测试计划只能有一个cookie管理器,当多个manager同时存在时,无法指定是用的哪一个manager.如果想让cookie manager跨域使用,修改JMeter.proper ...

  4. Jmeter--HTTP Cookie管理器

    一.什么情况下需要用到Cookie 一般情况下对于HTTP请求的用户登入操作,需要用到Cookie来模拟用户操作,或者对一些业务只有在用户登入之后才能进行操作,比如:常见的场景有购买商品.下单.支付等 ...

  5. JMeter学习-012-JMeter 配置元件之-HTTP Cookie管理器-实现 Cookie 登录

    前文我们讲过了若何获取登录后的 Cookie 信息,不知如何获取登录 Cookie 的朋友,敬请参阅我之前写的博文:Fiddler-005-获取 Cookie 信息.参阅上篇文章,获取到 Cookie ...

  6. lession2:使用HTTP Cookie 管理器来传递cookies值

    在实际进行压力测试的时候,经常会出现使用cookie传递值的情况,此时就需要使用[HTTP Cookie 管理器]来传递cookie值. 1.参照lession1中,创建线程组.sampler及聚合报 ...

  7. jmeter cookie管理器 使用方法---新手学习记录1

    首先得抓包: 我已post方法为例: POST /api/datasources/lemontest/jaql HTTP/1.1 Host: 192.168.1.107:8081 Content-Le ...

  8. Cookie管理 WebView同步

    NoHttp的Cookie管理原理 在文档的初始化配置一章讲了NoHttp如何配置或者禁用cookie自动管理. NoHttp的Cookie自动维护,严格遵守Http协议,即区分临时Cookie和有效 ...

  9. Jmeter之HTTP Cookie 管理器

    Jmeter所支持的Cookie标准有很多,同时jmeter也提供两组程序实现这些cookie标准,分别是httpclient3与httpclient4.http cookie 管理器中的Implem ...

  10. Node.js Cookie管理

    Cookie 管理 我们可以使用中间件向 Node.js 服务器发送 cookie 信息,以下代码输出了客户端发送的 cookie 信息: var express=require('express') ...

随机推荐

  1. 柯南君:看大数据时代下的IT架构(3)消息队列之RabbitMQ-安装、配置与监控

    柯南君:看大数据时代下的IT架构(3)消息队列之RabbitMQ-安装.配置与监控 一.安装 1.安装Erlang 1)系统编译环境(这里采用linux/unix 环境) ① 安装环境 虚拟机:VMw ...

  2. android:configChanges 屏幕横竖屏切换

    出处:http://blog.csdn.net/djy1992/article/details/9378195 --->  android:screenOrientation="por ...

  3. jQuery Pagination Plugin ajax分页控件

    <html> <body> <div id="datagrid"> </div> <div id="paginati ...

  4. umount.nfs device busy day virsh extend diskSpace, attachDisk

    KVM中linux虚拟机的硬盘添加方法 最近虚拟机中运行的东西比较多,很多.而刚启动的时候虚拟机分配的磁盘比较少,随着日志还有平时的上传文件的积累,磁盘空间报警了.网上查了下资料,自己也做了下实验.总 ...

  5. #include <thread>

    1 detach 脱离当前主线程,自由执行,乱序; 2 join() 等待模式,执行完再执行下一个 3 std::this_thread::get_id() 获取当前线程编号 4 std::threa ...

  6. HTML5阴影与渐变

    一.阴影 阴影的效果,阴影有四个状态值控制,分别是shadowBlur,shadowOffsetX,shadowOffsetY和shadowColor.shadowBlur为阴影的像素模糊值,shad ...

  7. Oracle死锁。

    oracle数据库死锁一般情况下在oracle数据库中不会.但是在程序中可以开启事物没有提交,但是程序报错我们就关了程序在重新调试.但是我们程序总是在执行 comm.ExecuteNonQuery() ...

  8. JavaSE思维导图(七)

  9. Win8.1重装win7或win10中途无法安装

    一.有的是usb识别不了,因为新的机器可能都是USB3.0的,安装盘是Usb2.0的. F12更改系统BIOS设置,我改了三个地方: 1.设置启动顺序为U盘启动 2.关闭了USB3.0 control ...

  10. NOPI导出Excel 自定义列名

    NOPI 做Excel 导出确实很方便 ,但是一直在用没好好研究. 在网上没找到自定义Columns的方法 ,于是乎自己就在原来的方法上简单地改改. 想用的童鞋们可以直接拿去用! /// 数据大于65 ...