Cookie与Session的研究学习

Cookie与Session的研究学习


Cookie介绍

Cookie是浏览器(User Agent)访问一些网站时,这些网站存放在客户端的一组数据,用于使网站等跟踪用户,实现用户自定义功能。可以设置Cookie的生存时间,如果不设置其生命周期就是会话的生命周期。Cookie对象是存在客户端(浏览器)上的。

备注:Cookie主要的应用场景就是方便服务器追踪用户,还有用户自动登入功能。

Cookie在java servlet中使用

Cookie核心方法
//Constructs a cookie with the specified name and value.
//初始化一个Cookie对象
public Cookie(String name, String value) {

    this.name = name;
    this.value = value;

}

//Sets the maximum age in seconds for this Cookie.
//设置Cookie的最大存活时间
public void setMaxAge(int expiry) {
    maxAge = expiry;
}
...还有很多其它的方法比如设置域名、设置path等

获取与增加Cookie到浏览器

//Returns an array containing all of the Cookie objects the client sent with this request.
//使用HttpServletRequest对象获取Cookie对象
public Cookie[] getCookies();


//Adds the specified cookie to the response.  This method can be called multiple times to set more than one cookie.
//使用HttpServletResponse对象向浏览器中增加Cookie对象
public void addCookie(Cookie cookie);

//备注:通常浏览器对同一个域名的Cookies增加数量是有限制的通常是20个。

简单使用代码

@WebServlet(urlPatterns = "/cookieServlet")
public class CookieServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //其实在默认情况下(不要禁止Cookie)只要浏览器访问任何网站都会有一个Cookie对象,name为JESSIONID,value为32位长字符串。
        //这也说明了session实现底层是cookie的支持
        Cookie[] cookies = req.getCookies();
        if (cookies != null) {
            for (Cookie cookie : cookies) {
                System.out.println("name:" + cookie.getName() + "------->value:" + cookie.getValue());
            }
        }
        //创建Cookie对象并使用response增加到浏览器中
        Cookie cookie = new Cookie("hello", "helloworld");
        cookie.setMaxAge(10);
        resp.addCookie(cookie);
    }
}
//由此可见我们可以做用户自动登入,当用于选择了保存密码是,我们可以根据
我们之前增加的cookie的name获取cookie对象,如果cookie对象还有效的话,
就认为登入成功,如果失效了即不存在了就重新登入。

Session介绍

Session是存放在服务器端的类似于HashTable结构(每一种Web开发技术的实现可能不一样,下文直接称之为HashTable)来存放用户 数据,当浏览器第一次发送请求时,服务器自动生成了一个HashTable和一个Session ID用来唯一标识这个HashTable,并将其通过响应发送到浏览器。

Session的客户端实现形式(即Session ID的保存方法)

1、使用Cookie来保存,这是最常见的方法
2、 使用URL附加信息的方式,也就是像我们经常看到JSP网站会有xxx.jsp?JSESSIONID=*一样的.(cookie禁用时可采用)
3、第三种方式是在页面表单里面增加隐藏域
备注:绝大多数我们只管用Cookie来实现即可。

备注:Session主要的应用场景也是跟踪用户状态,比如购物车功能、防止表单重复提交。

Session在java servlet中使用

Session核心方法

//Binds an object to this session, using the name specified.If
an object of the same name is already bound to the session,the
 object is replaced.
//赋值操作
public void setAttribute(String name, Object value);

//Returns the object bound with the specified name in this 
session, or null if no object is bound under the name.
//取值操作
public Object getAttribute(String name);

//Specifies the time, in seconds, between client requests before
 the servlet container will invalidate this session. 
//设置session有效时间
public void setMaxInactiveInterval(int interval);

//Invalidates this session then unbinds any objects bound to it.
//使session失效 
public void invalidate();

//备注:通过Request对象可以获得Session对象。

Cookie和Session的联系

Cookie 可以让服务端程序跟踪每个客户端的访问,但是每次客户端的访问都必
须传回这些 Cookie,如果 Cookie 很多,这无形地增加了客户端与服务端的数
据传输量,而 Session 的出现正是为了解决这个问题。

同一个客户端每次和服务端交互时,不需要每次都传回所有的 Cookie 值,而是
只要传回一个 ID,这个 ID 是客户端第一次访问服务器的时候生成的,而且每
个客户端是唯一的。这样每个客户端就有了一个唯一的 ID,客户端只要传回这个
ID 就行了,这个 ID 通常是 NANE 为 JSESIONID 的一个 Cookie。

Cookie 和 Session 都是为了保持用户访问的连续状态,之所以要保持这种状
态,一方面是为了方便业务实现,另一方面就是简化服务端程序设计,提高访问
性能,但是这也带来了另外一些挑战,如安全问题、应用的分布式部署带来的 
Session 的同步问题及跨域名 Session 的同步等一系列问题。本章分析了
Cookie 和 Session 的工作原理,并介绍了一致分布式 Session 的解决方案。

参考

https://www.ibm.com/developerworks/cn/java/books/javaweb_xlb/10/
https://www.zhihu.com/question/19786827
http://blog.csdn.net/fangaoxin/article/details/6952954

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值