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') ...
随机推荐
- 7816的报文结构——APDU
命令APDU 包括头和主体(这可以在上面的图中看到).头包括CLA,INS,P1 和P2 域.同T0 协议一样,CLA 和INS 说明了应用的分类和指令.P1 和P2 用来详细说明具体指令,并由每一条 ...
- MyEclipse配色字体等配置的解决方案
Myeclipse黑色配色方案(精心修改版) http://download.csdn.net/detail/rehongchen/6579945 如何改变Myeclipse编辑区背景色 http:/ ...
- Delphi XE7中新并行库
Delphi XE7中添加了新的并行库,和.NET的Task和Parellel相似度99%. 详细内容能够看以下的文章: http://www.delphifeeds.com/go/s/119574 ...
- android 回调
调函数(callback Function),顾名思义,用于回调的函数. 回调函数只是一个功能片段,由用户按照回调函数调用约定来实现的一个函数.回调函数是一个工作流的一部分,由工作流来决定函数的调用 ...
- web - float , 浮动
浮动 : 使元素脱离文档流,按照指定的方向发生移动,遇到父级边界或者相邻的浮动元素则停下来: 元素被设置浮动属性后,呈现的特征有: 1.多个块可以在一行显示 2.内联元素支持狂傲 3.默认宽度由内容撑 ...
- 使用sqlplus批量执行脚本的总结
当然,我们可以在plsql中执行,但是在实际生产环境中,可能更多的是使用简便的sqlplus.步骤如下: 1.登陆client sqlplus connect <username>/< ...
- PDF417码制尺寸定义
PDF417码制尺寸定义 1.模块宽度(X)不得小于0.191mm,你们目前所用300dpi打印机点的尺寸是0.084 mm * 0.01 mm,可 三倍以上值 2.条 ...
- Jsp连接Mysql数据库取数方法
我将Jsp连接Mysql数据库方法整理如下,供大家学习交流! 1.首先在myslq数据库中新建mldn数据库,并新建emp表.(方法不展开介绍) 插入数据如下: create table `emp` ...
- 如何为你的美术妹子做Unity的小工具(一)
在上的工具栏添加 也就是这个位置
- no data type for node
java.lang.IllegalStateException: No data type for node: org.hibernate.hql.ast.tree.IdentNode \-[IDE ...