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());
}
}
}