Cookie & Session

2017-01-04  本文已影响13人  ibyr

Cookie

API: javax.servlet.http.Cookie

//to get cookie from client
Cookie[] cookies = request.getCookie();
// to write cookie  一个Cookie对象保存一个键值对
response.addCookie(Cookie cookie);

如下是Baidu的Cookie:


BaiduCookie.png

Session

API: javax.servlet.http.HttpSession

// get session object.
HttpSession session = request.getSession();
// set session attribute
session.setAttribute("logintime", new Date());

// get session attribute
session.getAttribute("logintime");

// Attention to difference.
request.getSession(boolean create);  // true 创建Session之后再返回
request.getSession();   //返回session,不存在则返回null。
上一篇下一篇

猜你喜欢

热点阅读