Cookie管理
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管理的更多相关文章
- JMeter中HTTP Cookie 管理器使用
案例: 在一次做公司OA系统的时候,发现录制脚本无法回放成功,通过定位,是因为登录的过程中存在重定向,导致登录接口的状态没有自动带入重定向页面 解决方法: 加入HTTP Cookie 管理器使用 现象 ...
- nodejs cookie管理
Cookie 管理 我们可以使用中间件向 Node.js 服务器发送 cookie 信息,以下代码输出了客户端发送的 cookie 信息: // express_cookie.js 文件 var ex ...
- JMeter HTTP Cookie管理器的跨域使用
Jmeter的一个测试计划只能有一个cookie管理器,当多个manager同时存在时,无法指定是用的哪一个manager.如果想让cookie manager跨域使用,修改JMeter.proper ...
- Jmeter--HTTP Cookie管理器
一.什么情况下需要用到Cookie 一般情况下对于HTTP请求的用户登入操作,需要用到Cookie来模拟用户操作,或者对一些业务只有在用户登入之后才能进行操作,比如:常见的场景有购买商品.下单.支付等 ...
- JMeter学习-012-JMeter 配置元件之-HTTP Cookie管理器-实现 Cookie 登录
前文我们讲过了若何获取登录后的 Cookie 信息,不知如何获取登录 Cookie 的朋友,敬请参阅我之前写的博文:Fiddler-005-获取 Cookie 信息.参阅上篇文章,获取到 Cookie ...
- lession2:使用HTTP Cookie 管理器来传递cookies值
在实际进行压力测试的时候,经常会出现使用cookie传递值的情况,此时就需要使用[HTTP Cookie 管理器]来传递cookie值. 1.参照lession1中,创建线程组.sampler及聚合报 ...
- jmeter cookie管理器 使用方法---新手学习记录1
首先得抓包: 我已post方法为例: POST /api/datasources/lemontest/jaql HTTP/1.1 Host: 192.168.1.107:8081 Content-Le ...
- Cookie管理 WebView同步
NoHttp的Cookie管理原理 在文档的初始化配置一章讲了NoHttp如何配置或者禁用cookie自动管理. NoHttp的Cookie自动维护,严格遵守Http协议,即区分临时Cookie和有效 ...
- Jmeter之HTTP Cookie 管理器
Jmeter所支持的Cookie标准有很多,同时jmeter也提供两组程序实现这些cookie标准,分别是httpclient3与httpclient4.http cookie 管理器中的Implem ...
- Node.js Cookie管理
Cookie 管理 我们可以使用中间件向 Node.js 服务器发送 cookie 信息,以下代码输出了客户端发送的 cookie 信息: var express=require('express') ...
随机推荐
- C语言入门(6)——C语言常用数学函数
在编码过程中会经遇到数学运算,幸运的是C语言提供了非常丰富的数学函数库. 在数学中使用函数有时候书写可以省略括号,而C语言要求一定要加上括号,例如sin(pi/2)这种形式.在C语言的术语中,pi/2 ...
- 安卓activity捕获返回button关闭应用的方法
安卓activity捕获返回button关闭应用的方法 @Override public boolean onKeyDown(int keyCode, KeyEvent event) { //按下键盘 ...
- jQuery为多个元素绑定相同的事件
方式一: // 假设$("#div1", "#divN")有多个对象$("#div1", "#divN").each(f ...
- JavaScript之向文档中添加元素和内容的方法
一.非DOM方法添加 1.document.write() <html xmlns="http://www.w3.org/1999/xhtml"> <head&g ...
- Ghost Button制作教程及设计趋势分析
概述:Ghost Button(虚拟按钮)是网页设计中一个非常实用的按钮样式,特别是图片背景中,有出色的效果.今天我们一起来研究Ghost Button的各种效果的制作方法,并对Ghost Butto ...
- SQL Convert XML to Table
将xml nodes 属性中的值 转为table 形式 declare @xml2 xml set @xml2 = '<CMADatas> <CMAData CmaName=&quo ...
- EasyUI easyui-combobox 重复发送请求
今天在做个EasyUI easyui-combobox 下拉框动态连级的时候发现在选择一个值的时候发送了两次请求,这有点逆天呀~!!反人类~!必须和谐一开始以为是重复绑定了,重新审核了代码 确定没有~ ...
- iOS开发篇-AFNetworking 上传和下载
最近用到了关于AFNetworking的上传和下载问题,顺便写到博客中,以供大家参考和研究. //下载NSURLSessionConfiguration *configuration = [NSURL ...
- 在MAC下使用github
一 安装git 这个咱们就不说了,一般安装xcode都会自带git 如果不知道有没有下载git, 在终端运行 $which git 会打印出git的安装路径 /usr/local/bin/gi ...
- HOJ题目分类
各种杂题,水题,模拟,包括简单数论. 1001 A+B 1002 A+B+C 1009 Fat Cat 1010 The Angle 1011 Unix ls 1012 Decoding Task 1 ...