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') ...
随机推荐
- LDA的一些资料
LDA-math-汇总 LDA数学八卦 http://www.52nlp.cn/lda-math-%E6%B1%87%E6%80%BB-lda%E6%95%B0%E5%AD%A6%E5%85%AB%E ...
- poj2840
#include <stdio.h> #include <stdlib.h> #include<string.h> int main() { int n,len; ...
- paip.c++ qt 项目工程互相引用的方法
paip.c++ qt 项目工程互相引用的方法 作者Attilax , EMAIL:1466519819@qq.com 来源:attilax的专栏 地址:http://blog.csdn.net/ ...
- 加入收藏夹的js代码(求兼容chrome浏览器的代码)
从网上找了加入收藏夹的js代码,但不兼容chrome,不知道有没有兼容chrome的相关代码,希望有知道的告诉一下,谢谢! 代码如下 $("#id").click(function ...
- HP DL360 G7通过iLO部署系统
HPDL360 G7通过iLO部署系统 HP DL360 G7是没有光驱的服务器,可使用USB外置光驱.PXE网络安装.ILO方式的安装操作系统 一.HP iLO 简介 iLO 是一组芯片,内部是vx ...
- Windows Server 2003 安装Sql Server 2005 问题处理
安装途中遇到: 问题1.无法找到产品Microsoft SQL Server Native Client的安装程序包.请使用安装包sqlncli.msi的有效副本重新安装? 答:安装SQL Serve ...
- CentOS6.4下安装JDK1.6
首先,切换到jdk安装包所在目录: [rot@centos6 Downloads]# ./jdk-6u45-linux-x64-rpm.bin Unpacking... Checksumming... ...
- headfirst之装饰模式
class A A.hello class B extends A B.hello = A.hello+B 装饰模式:子类对父类想要包装的方法进行重写,使之成为加强版
- BZOJ 1212: [HNOI2004]L语言( dp + trie )
因为单词很短...用trie然后每次dp暴力查找...用哈希+dp应该也是可以的.... ------------------------------------------------------- ...
- 原生js动态改变dom高度
item参数为要改变高度的dom,maxHight参数为dom的最大高度,speed参数为改变高度的速度function addHeight(item,maxHight,speed){ var ite ...