1、在web容器中设置

例如:tomcat里面的web.xml

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

默认30分钟,自己修改数字即可

 

2、在工程的web.xml中设置

例如:java web 项目中,在WebContent->WEB-INF->web.xml下

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

3、在Servlet中设置(Java代码)

HttpSession session = request.getSession();

session.setMaxInactiveInterval(30*60);//以秒为单位

以上三种优先顺序:1 < 2 <3

 

 

备注:

request.getSession()和request.getSession(true)意思相同:获取session,如果session不存在,就新建一个

reqeust.getSession(false)获取session,如果session不存在,则返回null

如果 项目中无法确定会话一定存在,最好用request.session(false);

例如:当向Session中存取登录信息时,一般建议:HttpSession session =request.getSession();

          当从Session中获取登录信息时,一般建议:HttpSession session =request.getSession(false);

posted on 2017-08-17 09:37  zhouerba  阅读(2707)  评论(0编辑  收藏  举报