Cookie-学习笔记

1、Cookies
对Cookies的理解:
简单点说 Cookies 就是数据传递的一种方式,目的是保存数据到客户端。

Creates a cookie, a small amount of information sent by a servlet to a Web browser, saved by the browser, and later sent back to the server. A cookie's value can uniquely identify a client, so cookies are commonly used for session management.
A cookie has a name, a single value, and optional attributes such as a comment, path and domain qualifiers, a maximum age, and a version number. [color=red]Some Web browsers have bugs in how they handle the optional attributes, so use them sparingly to improve the interoperability of your servlets. [/color]

2、Cookies保存值的方式
Cookies 保存值是以键值对的形式来保存,Cookie(String name, String value)
通常使用 getName() 和 getValue() 来取出Cookies中的键值对信息。

3、在HttpServletRequest 获得Cookies 对象
在HttpServletRequest.getCookies() 方法得到的是 Cookies[],需要遍历取到Cookies对象。

4、描述cookie和session的作用,区别和各自的应用范围,session工作原理?
Cookie:主要用在保存客户端,其值在客户端与服务端之间传送,不安全,存储数据量有限;
Session:主要在保留服务端,每个session在服务端都一个id做为标识,存储数据量大,安全性高,占用服务端的内存资源。


重点:
1、servlet 向用户浏览器发送Cookies,客户端注入Cookies HttpServletResponse.addCookie(javax.servlet.http.Cookie)
2、浏览器返回一个Cookies 向servlet ,服务端读取Cookies
HttpServletRequest.getCookies()
3、API中强调使用Cookies时存在Bugs 需小心使用。

代码重点:

Cookie cookie = new Cookie();
cookie.setMaxAge(60); //设置最大时间
cookie.setValue(null);
response.addCookie(cookie); //这句是重点,之前全部是在服务器端对Cookie的操作,这句的意思是将以上对该Cookie的操作返回给客户端,这样对Cookie的操作才真正有意义。



代码实例:


import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class TestSetCookies extends HttpServlet {
// 设置Cookie

// 1:服务器可以向客户端写内容
// 2:只能是文本内容
// 3:客户端可以阻止服务器写入
// 4:只能拿自己webapp写入的东西
// 5:Cookie分为两种,第一种:属于窗口/子窗口(放在内存中的),第二种:属于文本(有生命周期的)
// 6:一个servlet/jsp设置的cookies能够被同一个路径下面或者子路径下面的servlet/jsp读到 (路径 = URL)(路径 !=
// 真实文件路径)

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
req.setCharacterEncoding("GBK");
resp.setCharacterEncoding("GBK");
// Cookie(String name, String value)
for (int i = 0; i < 10; i++) {
Cookie cookie = new Cookie("NO." + i + "Cookies", i + " Cookies");
cookie.setMaxAge(60); //设置最大时间
resp.addCookie(cookie); //加入Cookies

}
PrintWriter out = resp.getWriter();
out.println("<html><head><title>设置Cookie</title></head>"
+ "<BODY>\n"
+ "<H1 ALIGN=\"CENTER\">"
+ "设置Cookie" + "</H1>\n"
+ "Cookie\n"
+ "<A HREF=\"TestShowCookies\">\n"
+ "查看</A>.\n"
+ "</BODY></HTML>");
}
}




import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.*;
import javax.servlet.http.*;


public class TestShowCookies extends HttpServlet {
//读取客户端的Cookie
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
req.setCharacterEncoding("GBK");
resp.setCharacterEncoding("GBK");
Cookie[] cookies = req.getCookies();

PrintWriter out = resp.getWriter();
for(int i=0;i<cookies.length;i++){
Cookie cookie = cookies[i];
out.print("第"+i+"个 cookis Name:"+cookie.getName());
out.println("Value :"+cookie.getValue());
}
}
}




response.add(cookies);//这是是向客户机写入Cookies
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值