初步认识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 ...
随机推荐
- Linux中查看进程和杀掉进程
ps -A查看正在运行的所有进程 kill -9 xxx杀掉某个进程,xxx为进程ID.
- mybatis单笔批量保存
在上一篇写了接口调用解析返回的xml,并赋值到实体.这一篇主要介绍,如何保存实体数据. 一,xml样例 <?xml version="1.0" encoding=" ...
- 【跟着stackoverflow学Pandas】 -Get list from pandas DataFrame column headers - Pandas 获取列名
最近做一个系列博客,跟着stackoverflow学Pandas. 以 pandas作为关键词,在stackoverflow中进行搜索,随后安照 votes 数目进行排序: https://stack ...
- iOS-----使用addressBook管理联系人之修改联系人
使用addressBook管理联系人之修改联系人 修改联系人 修改联系人先从底层地址簿中加载一条ABRecordRef记录,然后对这条ABRecordRef记录的属性值进行修改,修改完成后把这条修改后 ...
- arcotg_udc: exports duplicate symbol imx_usb_create_charger (owned by kernel)
/********************************************************************************* * arcotg_udc: exp ...
- ZetCode PyQt4 tutorial widgets II
#!/usr/bin/python # -*- coding: utf-8 -*- """ ZetCode PyQt4 tutorial In this example, ...
- Swapping eth0 and eth1 on OK335xS board
/******************************************************************************* * Swapping eth0 and ...
- Windows 下的高 DPI 应用开发(UWP / WPF / Windows Forms / Win32)
本文将介绍 Windows 系统中高 DPI 开发的基础知识.由于涉及到坐标转换,这种转换经常发生在计算的不知不觉中:所以无论你使用哪种 Windows 下的 UI 框架进行开发,你都需要了解这些内容 ...
- 6-3 Add Two Polynomials(20 分)
Write a function to add two polynomials. Do not destroy the input. Use a linked list implementation ...
- 《DSP using MATLAB》示例 Example 9.11
代码: %% ------------------------------------------------------------------------ %% Output Info about ...