1.jsp中操作session
1(String)request.getSession().getAttribute("username"); // 获取 2request.getSession().setAttribute("username", "xxx"); // 设置
2.java中操作session
1//servlet中 2request.getSession(); 3session.getAttribute("username"); 4session.setAttribute("username", "xxx"); 5session.setMaxInactiveInterval(30*60); 6session.invalidate(); 7 8//struts中方法1 9ServletActionContext.getRequest().getSession().setAttribute("username", "xxx"); 10ServletActionContext.getRequest().getSession().getAttribute("username"); 11ServletActionContext.getRequest().getSession().setMaxInactiveInterval(30*60); 12ServletActionContext.getRequest().getSession().invalidate(); 13 14//struts中方法2 15ActionContext.getContext().getSession().put("username", "xxx"); 16ActionContext.getContext().getSession().get("username"); 17ActionContext.getContext().getSession().clear();
3.web.xml中操作session
1<session-config> 2 <session-timeout>30</session-timeout> 3</session-config>
4.tomcat-->conf-->conf/web.xml
1<session-config> 2 <session-timeout>30</session-timeout> 3</session-config>
总结:优先级比较,java中写的要比web.xml中的高。