session在单机服务器中是很重要的。可以用于存放当前登录用户信息、权限等信息
工具/原料
idea
获取并向session中添加数据
1、创建java web项目 。这里使用的是spring boot创建的 。可以参考一下:两篇文章创建
2、创建存放session的整体方法如下:
3、HttpSession session = request.getSession(); //使用request对象的getSession()获取session,如果session不存在则创建一个
4、session.setAttribute("data", "孤傲苍狼"); //将数据存储到session中
5、启动项目,使用postman进行添加操作测试:
获取并从session中获取数据
1、HttpSession session = request.getSession();tStudent student =(tStudent像粜杵泳)session.getAttribute("student");String jsonStr = JSON.toJSONString(student);out.write("从session中获取到的学生对象为:"+jsonStr);
2、使用postman进行获取session测试