一、Session简介
Session技术是将数据存储在服务器端的技术,会每个客户端都创建一块内存空间存储客户的数据,但客户端需要都携带一个标识ID去服务器中寻找属于自己的内存空间。所以说Session的实现是基于Cookie,Session需要借助于Cookie存储客户的唯一性标识JSESSIONID
二、Session获取
1、获得Session对象
HttpSession session = request.getSession();
此方法会获得专属于当前会话的Session对象,如果服务器端没有该会话的Session对象会创建一个新的Session返回,如果已经有了属于该会话的Session直接将已有的Session返回(实质就是根据JSESSIONID 判断该客户端是否在服务器上已经存在 Session)
三、Session存取数据&生命周期
1、怎样向session中存取数据(session也是一个域对象),有如下三个方法
session.setAttrbute(String name, Object obj);
session.getAttrbute(String name);
session.removeAttrbute(String name);
2、Session对象的生命周期
- 创建:第一次执行 request.getSession()时创建
- 销毁:
服务器(非正常)关闭时
session过期/失效(默认30分钟)
可以在工程的web.xml中进行配置
<session-config>
<session-timeout>30</session-timeout>
</session-config>
四、Session的持久化
由于Session的创建和获取是取决于Cookie中的JSESSIONID决定的,所以如果Cookie被清除了,服务器就无法找到对应的Session了,因此如果想要持久化Session就必须对Cookie中的JSESSIONID进行持久化
五、Java Servlet Session 实例
1、创建Session
@WebServlet(name = "SessionServlet",urlPatterns = "/session")
public class SessionServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//第一次访问时没有session,服务器会自动创建一个session对象
//之后再次访问的时候已经存在了session对象,这个直接获取这个对象
HttpSession session = request.getSession();
String sessionId = session.getId();
response.getWriter().write("JESSIONID="+sessionId);
}
}
2、保存Session
@WebServlet(name = "SaveSessionServlet",urlPatterns = "/saveSession")
public class SaveSessionServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession();
//创建一个cookie覆盖之前服务器自动生成的JSESSIONID
Cookie cookie = new Cookie("JSESSIONID", session.getId());
//设置持久化时间
cookie.setMaxAge(60*60);
response.addCookie(cookie);
session.setAttribute("goods","cup");
}
}
3、获取Session
@WebServlet(name = "GetSessionServlet",urlPatterns = "/getSession")
public class GetSessionServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取session
HttpSession session = request.getSession();
String goods = (String) session.getAttribute("goods");
response.getWriter().write(goods+"");
}
}
六、购物车的简单应用
1、添加购物车
@WebServlet(name = "AddCartServlet",urlPatterns = "/addCart")
public class AddCartServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1 获取到商品的信息
String name = request.getParameter("name");
// 2 需要保存商品信息到session
HttpSession session = request.getSession();
List list = (List) session.getAttribute("list");
//第一次访问的时候list不存在,需要创建一个list
if (list==null){
list=new ArrayList();
}
list.add(name);
session.setAttribute("list",list);
// session的持久化操作,也就是持久化JSESSIONID
Cookie cookie = new Cookie("JSESSIONID", session.getId());
cookie.setMaxAge(60*60*24);
cookie.setPath("/hello");
response.addCookie(cookie);
}
}
2、前端简单展示
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<center>
<h1>商品列表</h1>
<a href="/hello/addCart?name=杯子">杯子</a><br>
<a href="/hello/addCart?name=书包">书包</a><br>
<a href="/hello/addCart?name=笔记本">笔记本</a><br>
</center>
</body>
</html>
3、获取购物车数据
@WebServlet(name = "GetCartServlet",urlPatterns = "/getCart")
public class GetCartServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession();
List<String> list= (List<String>) session.getAttribute("list");
response.setContentType("text/html;charset=utf-8");
for (String s : list) {
response.getWriter().write(s+"<br/>");
}
}
}