Servlet第五课:Cookie的使用
目标规划:
通过这一节课,我们能够懂得怎样使用Cookie。以及怎样获取Cookie中的内容。
插播广告:博客之星评选。点击投我一票。谢谢。
Cookie的具体概述。
1. Cookie 是保存在client的一个“键-值”对,用来标识用户的一些信息。
2. Cookie的应用
–在电子商务会话中标识用户
–对网站进行定制
–定向广告
3. 调用Cookie的构造函数,给出cookie的名称和cookie的值,二者都是字符串
Cookie c = new Cookie("userID", "a1234");
4. 假设要告诉浏览器将cookie存储到磁盘上。而非只保存在内存中,使用setMaxAge (參数为秒数)
c.setMaxAge(60*60*24*7); // One week
5. 将Cookie放入到HTTP响应
response.addCookie(c);
6. 调用request.getCookies 获得这会得到Cookie对象组成的数组,在这个数组中循环。调用每一个对象的getName。直到找到想要的cookie为止。
7.实例,创建cookie
我们首先创建一个Cookie信息:
核心代码:
Cookie c = new Cookie("goxuexi", "www.goxuexi.com");
c.setMaxAge(60*60*24*7);
response.addCookie(c);
所有代码:TestCookieServlet.java
package com.goxuexi.demo; import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; /**
* Servlet implementation class TestCookieServlet
*/
@WebServlet("/TestCookieServlet")
public class TestCookieServlet extends HttpServlet {
private static final long serialVersionUID = 1L; /**
* @see HttpServlet#HttpServlet()
*/
public TestCookieServlet() {
super();
// TODO Auto-generated constructor stub
} /**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
Cookie c = new Cookie("goxuexi", "www.goxuexi.com");
c.setMaxAge(60*60*24*7);
response.addCookie(c);
} /**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
} }
02.写一个获取Cookie的代码:
TestCookie.java
核心代码:
Cookie[] cs = request.getCookies();
for (Cookie c : cs) {
System.out.println(c.getName()+":"+c.getValue());
}
所有代码:
package com.goxuexi.demo; import java.io.IOException; import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; /**
* Servlet implementation class TestCookie
*/
@WebServlet("/TestCookie")
public class TestCookie extends HttpServlet {
private static final long serialVersionUID = 1L; /**
* @see HttpServlet#HttpServlet()
*/
public TestCookie() {
super();
// TODO Auto-generated constructor stub
} /**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
Cookie[] cs = request.getCookies();
if(cs != null)
for (Cookie c : cs) {
System.out.println(c.getName()+":"+c.getValue());
}
}
} /**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
} }
8.实例-使用cookie检測初訪者
//使用cookie检測初訪者
String result=null;
boolean newUser = true;
Cookie[] cookies = request.getCookies();
if(cookies!=null){
for(int i=0;i<cookies.length;i++){
Cookie c = cookies[i];
if((c.getName().equals("repeatVisitor"))&&(c.getValue().equals("yes"))){
newUser = false;
break;
}
}
} if (newUser) {
Cookie returnVisitorCookie = new Cookie("repeatVisitor", "yes");
returnVisitorCookie.setMaxAge(60 * 60 * 24 * 365);
response.addCookie(returnVisitorCookie);
result = "Welcome Aboard";
} else {
result = "Welcome Back";
}
System.out.println(result);
Servlet第五课:Cookie的使用的更多相关文章
- NeHe OpenGL教程 第四十五课:顶点缓存
转自[翻译]NeHe OpenGL 教程 前言 声明,此 NeHe OpenGL教程系列文章由51博客yarin翻译(2010-08-19),本博客为转载并稍加整理与修改.对NeHe的OpenGL管线 ...
- kali linux 渗透测试视频教程 第五课 社会工程学工具集
第五课 社会工程学工具集 文/玄魂 教程地址:http://edu.51cto.com/course/course_id-1887.html 目录 第五课社会工程学工具集 SET SET的社会工程 ...
- Adafruit的树莓派教程第五课:使用控制电缆
Adafruit的树莓派教程第五课:使用控制电缆 时间 2014-05-09 01:11:20 极客范 原文 http://www.geekfan.net/9095/ 主题 Raspberry PiM ...
- NeHe OpenGL教程 第三十五课:播放AVI
转自[翻译]NeHe OpenGL 教程 前言 声明,此 NeHe OpenGL教程系列文章由51博客yarin翻译(2010-08-19),本博客为转载并稍加整理与修改.对NeHe的OpenGL管线 ...
- NeHe OpenGL教程 第二十五课:变形
转自[翻译]NeHe OpenGL 教程 前言 声明,此 NeHe OpenGL教程系列文章由51博客yarin翻译(2010-08-19),本博客为转载并稍加整理与修改.对NeHe的OpenGL管线 ...
- NeHe OpenGL教程 第十五课:纹理图形字
转自[翻译]NeHe OpenGL 教程 前言 声明,此 NeHe OpenGL教程系列文章由51博客yarin翻译(2010-08-19),本博客为转载并稍加整理与修改.对NeHe的OpenGL管线 ...
- NeHe OpenGL教程 第五课:3D空间
转自[翻译]NeHe OpenGL 教程 前言 声明,此 NeHe OpenGL教程系列文章由51博客yarin翻译(2010-08-19),本博客为转载并稍加整理与修改.对NeHe的OpenGL管线 ...
- 斯坦福IOS开发第五课(第一部分)
转载请注明出处 http://blog.csdn.net/pony_maggie/article/details/27706991 作者:小马 因为第五课的内容比較多.分两部分来写. 一 屏幕旋转基本 ...
- Servlet的学习之Cookie
从本篇开始学习Servlet技术中的Cookie专题. 首先来了解什么是“会话”.会话是web技术中的一个术语,可以简单的理解为:用户打开一个浏览器,点击多个超链接,访问服务器多个web资源,然后关闭 ...
随机推荐
- vue-cli之webpack的proxyTable无效的解决方案
最近遇到这个需要单页访问跨域后台的问题 可以按照如下设置: proxyTable: { '/list': { target: 'http://api.xxxxxxxx.com', pathRewrit ...
- 造轮子和用轮子:快速入门JavaScript模块化
造轮子和用轮子:快速入门JavaScript模块化 前言 都说“不重复造轮子”,就像iPhone——它除了打电话还可以播放音乐——但是工程师不用从零开始做一个音乐播放功能,也许只要在iPhone的系统 ...
- 【mysql】autocommit=0后,commit, rollback无效
之前在[mysql]MySQLdb中的事务处理中用autocommit和commit()以及rollback()实现了事务处理. 但后来,用同样的代码在另一个数据库中运行却失败了.找了一个下午的原因. ...
- JMeter 聚合报告 90%响应时间
90%的响应时间理解 官方解释:90% Line - 90% of the samples took no more than this time. The remaining samples at ...
- Spring的核心之IoC容器创建对象
Spring的Ioc容器,是Spring的核心内容: 作用:对象的创建和处理对象的依赖关系. Spring容器创建对象有以下几种方式: 1:调用无参数的构造器 <!-- 默认无参的构造器 --& ...
- S2750&S5700&S6700 V200R003(C00&C02&C10) MIB参考
https://support.huawei.com/enterprise/docinforeader.action?contentId=DOC1000027337&idPath=791971 ...
- (第1篇)什么是hadoop大数据?我又为什么要写这篇文章?
摘要: hadoop是什么?hadoop是如何发展起来的?怎样才能正确安装hadoop环境? 这些天,有很多人咨询我大数据相关的一些信息,觉得大数据再未来会是一个朝阳行业,希望能尽早学会.入行,借这个 ...
- 详解webpack中的hash、chunkhash、contenthash区别
hash.chunkhash.contenthash hash一般是结合CDN缓存来使用,通过webpack构建之后,生成对应文件名自动带上对应的MD5值.如果文件内容改变的话,那么对应文件哈希值也会 ...
- BZOJ1071 [SCOI2007]压缩 其他
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ1071 题意概括 有两个序列a[1..n], b[1..n],其编号为1..n,设为s序列.现在我们 ...
- python中使用XPath笔记
XPath在Python的爬虫学习中,起着举足轻重的地位,对比正则表达式 re两者可以完成同样的工作,实现的功能也差不多,但XPath明显比re具有优势,在网页分析上使re退居二线. XPath介绍: ...