JavaEE中对Session操作

jsp中:

1 (String)request.getSession().getAttribute("username"); // 获取
2 request.getSession().setAttribute("username", "xxx");  // 设置

Java中:

 1 //servlet中
 2 request.getSession();
 3 session.getAttribute("username");
 4 session.setAttribute("username", "xxx");
 5 session.setMaxInactiveInterval(30*60);
 6 session.invalidate();
 7   
 8 //struts中方法1
 9 ServletActionContext.getRequest().getSession().setAttribute("username", "xxx");
10 ServletActionContext.getRequest().getSession().getAttribute("username");
11 ServletActionContext.getRequest().getSession().setMaxInactiveInterval(30*60); 
12 ServletActionContext.getRequest().getSession().invalidate();
13  
14 //struts中方法2
15 ActionContext.getContext().getSession().put("username", "xxx");
16 ActionContext.getContext().getSession().get("username");
17 ActionContext.getContext().getSession().clear();

web.xml中:

1 <session-config>  
2     <session-timeout>30</session-timeout>  
3 </session-config>

tomcat-->conf-->conf/web.xml中:

1 <session-config>
2     <session-timeout>30</session-timeout>
3 </session-config>

总结:优先级比较,java中写的要比web.xml中的高。

原文地址:https://www.cnblogs.com/chenhao1990/p/4628942.html