读书人

设立session 有效时间

发布时间: 2012-09-20 09:36:50 作者: rapoo

设置session 有效时间

<?xml version="1.0" encoding="UTF-8"?>  <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"      xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"      xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"      id="WebApp_ID" version="2.5">            <display-name>struts2_bar</display-name>      <welcome-file-list>          <welcome-file>index.html</welcome-file>      </welcome-file-list>        <!-- 设置session 有效时间 -->      <session-config>          <session-timeout>30</session-timeout>      </session-config>        <servlet>          <servlet-name>authImg</servlet-name>          <servlet-class>org.cric.util.AuthImg</servlet-class>      </servlet>      <servlet-mapping>          <servlet-name>authImg</servlet-name>          <url-pattern>/authImg</url-pattern>      </servlet-mapping>      <!--过滤器解析 *.jsp  因为要解析sturts标签-->      <filter>          <filter-name>struts</filter-name>          <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>      </filter>      <filter-mapping>          <filter-name>struts</filter-name>          <url-pattern>*.action</url-pattern>      </filter-mapping>      <filter-mapping>          <filter-name>struts</filter-name>          <url-pattern>*.jsp</url-pattern>      </filter-mapping>        </web-app>  



由于session值之前没有设置,以至于刚登录的网站,不到一分钟就超时了,总结了一下,原来是session过期的原因,以下是设置session时间的3个方法:

1. 在tomcat——>conf——>servler.xml文件中定义:


<Context path="/test" docBase="/test"     defaultSessionTimeOut="3600" isWARExpanded="true"     isWARValidated="false" isInvokerEnabled="true"     isWorkDirPersistent="false"/> 

defaultSessionTimeOut="3600"


2. 在web.xml中定义:


<session-config>   <session-timeout>20</session-timeout>   </session-config> 



3. 在程序中定义:

 session.setMaxInactiveInterval(30*60);


设置单位为秒,设置为-1永不过期

不知道这种设置会不会真正地起到作用的哦!

读书人网 >软件架构设计

热点推荐