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的更多相关文章

  1. Application对象、Session对象、Cookie对象、Server对象初步认识

    Application对象:记录应用程序参数的对象 用于共享应用程序级信息,即多个用户共享一个Application对象.在第一个用户请求ASP.NET文件时,将启动应用程序并创建Applicatio ...

  2. cookie的初步认识

    一.会话的概念 会话可简单理解为:用户开一个浏览器,点击多个超链接,访问服务器多个web资源,然后关闭浏览器,整个过程称之为一个会话. 有状态会话:一个同学来过教室,下次再来教室,我们会知道这个同学曾 ...

  3. JWT的初步了解以及session、cookie机制

    1.什么是状态保持? 想要了解JWT,首先需要知道什么是状态保持,举一个例子来说:无论是在web上还是在手机app上,我们都可以以游客的身份访问,此时都会有登录/注册字眼,当我们登录之后,就会是我们的 ...

  4. AIR 初步 Javascript学习之cookie操作

    //设置cookie的名称,值,过期时间         function setCookie(cookieName,cookieValue,cookieExpire) {             v ...

  5. cookie和session 的初步介绍

    Cookie和Session http协议不保存用户状态(信息) Cookie和Session都是为了能够保存用户信息 Cookie: 本质:保存在浏览器上的键值对 用途:标识当前用户信息 cooki ...

  6. selenium-webdriver(python) (十三) -- cookie处理

    本节重点: driver.get_cookies() 获得cookie信息 add_cookie(cookie_dict)  向cookie添加会话信息 delete_cookie(name)   删 ...

  7. Ajax初步理解

    最近在项目中经常会使用Ajax技术,用法上倒是熟练了,但是只知其然,不知其所以然,抽时间读了读JavaScript高级程序设计中关于Ajax的介绍有了些初步的理解,在此总结一下. 什么是Ajax Aj ...

  8. IE11下Forms身份认证无法保存Cookie的问题

    ASP.NET中使用Forms身份认证常见的做法如下: 1. 网站根目录下的Web.config添加authentication节点 <authentication mode="For ...

  9. [转]IE11下Forms身份认证无法保存Cookie的问题

    本文转自:http://www.cnblogs.com/jaxu/p/3698377.html ASP.NET中使用Forms身份认证常见的做法如下: 1. 网站根目录下的Web.config添加au ...

随机推荐

  1. bzoj1073

    题意: k短路 题解: A* 当然是抄了zzd的代码 然而需要特判 为什么把bool改成int爆空间!!! 代码: #include<bits/stdc++.h> using namesp ...

  2. Outpost Security Suite Pro 8.1 – 免费4个月

    OSS( 简称 )一款来自俄罗斯Agnitum公司的互联网安全产品. Outpost以网络防火墙知名,AVG和avast!等知名安全企业都有使用Outpost的防火墙技术. Outpost Secur ...

  3. New Concept English Two 13 31

    $课文29 出租汽车 294. Captain Ben Fawcett has bought an unusual taxi and has begun a new service. 本.弗西特机长买 ...

  4. Ubuntu系统配置apt-get软件更新源

    从别人那摘录的Ubuntu源,测的很好用 # 电子科技大学 (教育网) deb http://ubuntu.uestc.edu.cn/ubuntu/ oneiric main restricted u ...

  5. Spring map注入

    类test.Configurations定义如下 @Getter @Setter public class Configurations { private Map<AnswerSourceTy ...

  6. 《DSP using MATLAB》示例 Example 9.8

    代码: %% ------------------------------------------------------------------------ %% Output Info about ...

  7. [TopCoder11557]MatrixPower

    vjudge description 你有一个\(n \times n\)的矩阵\(A\),下标从\(0\)开始,其中\(A_{i,j}=di + q^j\). 给你\(d,q,n,k,s,t\),求 ...

  8. Mac: iTerm2使用

    From: http://www.cnblogs.com/noTice520/p/3190529.html 之前一直有朋友要我分享下在用的mac软件,今天有空就来写一下,可能不止于软件,会有一些配置或 ...

  9. Python学习思维导图

     刚学习Python时,边学边总结的,采用思维导图的形式, 适合回顾使用.内容参考<Python:从入门到实践>一书.   再给出一张Datacamp网站上的一张关于Python基础的总结 ...

  10. asp.net(C#)链接Oracle连接字符串

    在NET环境中链接Oracle数据库有两种组建链接方式: 1)使用OleDB组件是通过Oracle OleDB驱动程序(OraOLEDB.dll)连接和访问Oracle数据库2)使用System.Da ...