初步认识cookie
cookie是由服务器创建,客户端读取及保存它的
同类请求指的是资源路径相同
Cookie的默认路径绑定是所请求的资源路径绑定的 ,指定路径时必须要有项目名称(说明是哪个项目)
使用cookie时还要设置它的使用路径,一般是项目路径,比如
cookie = new Cookie("remember", username+":"+password);
cookie.setMaxAge(60*60*24*7);
cookie.setPath(request.getContextPath());
response.addCookie(cookie);//addCookie(cookie)方法会覆盖同名的cookie
package com.huawei.cookie;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class TestCookie01
*/
public class TestCookie01 extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public TestCookie01() {
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
this.doPost(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
/**
* Cookie 是一种 在浏览器段存储信息的技术 不宜在客户端存储大量数据 信息不安全 (可以加密)
*
* 它是由 服务器生成 回写到客户端 在由客户端去决定该怎么处理这个数据
*
* 默认情况下是存在内存中的
* 默认情况下的作用范围 是当前整个项目有效
*
*/
Cookie cookie = new Cookie("book", "Java");
Cookie cookie01 = new Cookie("book1", "Java1");
//要想存起来 一定要设置 他的有效期
//如果大于0 则会存储在文件中
//如果等于0 则会删除他自己
//如果小于0 则不会被存储起来 而只是存在于客户端的内存中
cookie.setMaxAge(60*60);
//cookie.setMaxAge(0);
//设置生效范围 只有 给定路径以及给定路径的字路径有效
cookie.setPath("/TestCookie/test/test/123");
//是设置的同名不同值的相应消息头
response.addCookie(cookie);
response.addCookie(cookie01);
//response.
response.addHeader("Set-Cookie", "book2=javascript");
}
}
package com.huawei.cookie;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class TestCookie02
*/
public class TestCookie02 extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public TestCookie02() {
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
this.doPost(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
//得到cookie
String cookie = request.getHeader("Cookie");
System.out.println(cookie);
服务器端获取并解析cookie
Cookie []cookies = request.getCookies();
System.out.println(cookies.length);
for(Cookie c:cookies){
System.out.println(c.getName()+":"+c.getValue());
}
}
}
初步认识cookie的更多相关文章
- Application对象、Session对象、Cookie对象、Server对象初步认识
Application对象:记录应用程序参数的对象 用于共享应用程序级信息,即多个用户共享一个Application对象.在第一个用户请求ASP.NET文件时,将启动应用程序并创建Applicatio ...
- cookie的初步认识
一.会话的概念 会话可简单理解为:用户开一个浏览器,点击多个超链接,访问服务器多个web资源,然后关闭浏览器,整个过程称之为一个会话. 有状态会话:一个同学来过教室,下次再来教室,我们会知道这个同学曾 ...
- JWT的初步了解以及session、cookie机制
1.什么是状态保持? 想要了解JWT,首先需要知道什么是状态保持,举一个例子来说:无论是在web上还是在手机app上,我们都可以以游客的身份访问,此时都会有登录/注册字眼,当我们登录之后,就会是我们的 ...
- AIR 初步 Javascript学习之cookie操作
//设置cookie的名称,值,过期时间 function setCookie(cookieName,cookieValue,cookieExpire) { v ...
- cookie和session 的初步介绍
Cookie和Session http协议不保存用户状态(信息) Cookie和Session都是为了能够保存用户信息 Cookie: 本质:保存在浏览器上的键值对 用途:标识当前用户信息 cooki ...
- selenium-webdriver(python) (十三) -- cookie处理
本节重点: driver.get_cookies() 获得cookie信息 add_cookie(cookie_dict) 向cookie添加会话信息 delete_cookie(name) 删 ...
- Ajax初步理解
最近在项目中经常会使用Ajax技术,用法上倒是熟练了,但是只知其然,不知其所以然,抽时间读了读JavaScript高级程序设计中关于Ajax的介绍有了些初步的理解,在此总结一下. 什么是Ajax Aj ...
- IE11下Forms身份认证无法保存Cookie的问题
ASP.NET中使用Forms身份认证常见的做法如下: 1. 网站根目录下的Web.config添加authentication节点 <authentication mode="For ...
- [转]IE11下Forms身份认证无法保存Cookie的问题
本文转自:http://www.cnblogs.com/jaxu/p/3698377.html ASP.NET中使用Forms身份认证常见的做法如下: 1. 网站根目录下的Web.config添加au ...
随机推荐
- EPANET头文件解读系列2——ENUMSTXT.H
在前一系统中介绍了text.h,回顾下,该文件包含了EPANET中所有字符串常量的定义,而ENUMSTXT.H文件则是以text.h中定义的字符串常量为基础,来对这些字符串常量进行合理的分组,形成字符 ...
- Socket编程中检测端口是否被占用
一般检测一个端口是否被占用的方法是看bind是否成功,其实在Windows中有两个API可以获取到当前系统端口的占用情况(GetTcpTable/GetUdpTable),利用这两个函 ...
- BZOJ4571: [Scoi2016]美味【主席树】【贪心】
Description 一家餐厅有 n 道菜,编号 1...n ,大家对第 i 道菜的评价值为 ai(1≤i≤n).有 m 位顾客,第 i 位顾客的期 望值为 bi,而他的偏好值为 xi .因此,第 ...
- WinForm获取当前路径汇总
Winform获取应用程序的当前路径的方法集合汇总,值得收藏备用 具体如下, //获取当前进程的完整路径,包含文件名(进程名). string str = this.GetType().Assembl ...
- 6-18 Two Stacks In One Array(20 分)
Write routines to implement two stacks using only one array. Your stack routines should not declare ...
- zz 跟风小结一下孕期~
http://www.newsmth.net/nForum/#!article/FamilyLife/1754069968 发信人: milkcat (牛奶猫养了一只牛奶猫(*^__^*) 嘻嘻……) ...
- centos 7 安装rabbitmq 3.6.12
0 安装 epel yum -y install http://dl.fedoraproject.org/pub/epel/7/x86_64/Packages/e/epel-release-7-11. ...
- js中caller和callee属性详解
一.caller属性返回一个对函数的引用,该函数调用了当前函数.functionName.callerfunctionName 对象是所执行函数的名称.说明对于函数来说,caller 属性只有在函数执 ...
- bzoj2660最多的方案
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2660 当然可以看出 选了第 i 个斐波那契数<=>选了第 i - 1 和第 i ...
- bzoj3143游走
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3143 学到了无向图中点被经过的期望次数和边被经过的期望次数. 一个点被经过的期望次数 就是 ...