JavaWeb开发技巧——SESSION

Session

(1) session是什么?

服务器端为了保存用户的状态而创建的一个特殊的对象(即session对象)。 当浏览器第一次访问服务器时,服务器会创建session对象(该象有一个唯一的id,一般称之为sessionId),接下来服务器会将sessionId以cookie的方式发送给浏览器。当浏览器再次访问服务器时,会将sessionId发送过来,服务器就可以依据sessionId找到对应的sessinon对象。

(2) session在servlet中的使用

创建

HttpSession session=request.getSession();
session.setAttribute("username", username);
// 简便写法:request.getSession().setAttribute("username", username);

获取

HttpSession session=request.getSession();
session.getAttribute("username");
// session=request.getSession().session.getAttribute("username");

 

posted on 2020-10-13 11:24  pjhhh  阅读(85)  评论(0编辑  收藏  举报