java web中session简单的使用

1.jsp中操作session

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

2.java中操作session

//servlet中
request.getSession();
session.getAttribute("username");
session.setAttribute("username", "xxx");
session.setMaxInactiveInterval(30*60);
session.invalidate();
 
//struts中方法1
ServletActionContext.getRequest().getSession().setAttribute("username", "xxx");
ServletActionContext.getRequest().getSession().getAttribute("username");
ServletActionContext.getRequest().getSession().setMaxInactiveInterval(30*60); 
ServletActionContext.getRequest().getSession().invalidate();

//struts中方法2
ActionContext.getContext().getSession().put("username", "xxx");
ActionContext.getContext().getSession().get("username");
ActionContext.getContext().getSession().clear();

3.web.xml中操作session

<session-config>  
    <session-timeout>30</session-timeout>  
</session-config>

4.tomcat-->conf-->conf/web.xml

<session-config>
    <session-timeout>30</session-timeout>
</session-config>

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

评论

h
hw_7864

⭐️ Admin快速开发: https://show.cool-admin.com/

赞 0
h
hw_7864

⭐️ 官方文档: https://cool-js.com

赞 0
h
hw_7864

⭐️ chatgpt在线体验: https://cool-js.com/ai/chat-vip/index.html?r=helloworld

赞 0