1、什么是session
服务器会为每一个用户(浏览器)创建一个seesion,这个session会一直持续到浏览器关闭
2、在web.xml中设置session自动失效的时间
<session-config>
<!--15分钟后session自动失效,以分钟为单位-->
<session-timeout>1</session-timeout>
</session-config>
3、创建session的代码
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
HttpSession session = request.getSession();
session.setAttribute("name","xiaolin");
String id = session.getId();
if(session.isNew()){
response.getWriter().write("session创建成功"+session);
}
else {
response.getWriter().write("session已经存在了");
}
}
4、得到session的值
HttpSession session = request.getSession();
Person name = (Person)session.getAttribute("name");
System.out.println(person.toString());