Tomcat/weblogic session失效时间的几种设置方法
2010年09月02日 星期四 下午 5:25

一、在容器中设置
tomcat中配置server.xml中定义context时采用如下定义:

<Context path="/livsorder" docBase="/home/httpd/html/livsorder"
defaultSessionTimeOut="3600" isWARExpanded="true"
isWARValidated="false" isInvokerEnabled="true"
isWorkDirPersistent="false"/>

weblogci中设置weblogic特有部署描述符weblogic.xml的<session-descriptor>元素的
timeoutsecs 属性。这个值以秒为单位
<session-descriptor>
<session-param>
<param-name>timeoutsecs</param-name>
<param-value>2600</param-value>
</session-param>
</session-descriptor>
默认值是2600秒

二、web应用设置,优先级比容器中高

设置web应用程序描述符web.xml里的<session-timeout>元素。这个值以分钟为
单位,并覆盖 weblogic.xml中的timeoutsecs属性
<session-config>
<session-timeout>24</session-timeout>
</session-config>
此例表示session将在24分钟后过期 ,默认设置是30分钟
当<session- timeout>设置为-2,表示将使用在weblogic.xml中设置的
timeoutsecs这个属性值。
当<session- timeout>设置为-1,表示session将永不过期,而忽略在
weblogic.xml中设置的timeoutsecs属性值。
该属性值可以通过console控制台来设置

三、应用代码中设置,优先级最高

jsp中控制
session.setmaxinactiveinterval(7200);
session是默认对象,可以直接引 用,单位秒s
4,servlet中控制
httpsession session = request.getsession();
session.setmaxinactiveinterval(7200);
单位秒 s


12:设置tomcat访问权限的问题,需要在应用工程的web.xml中增加
<web-app>
<security-constraint>
  <web-resource-collection>
      <display-name>Example Security Constraint</display-name>
      <web-resource-name>My Test</web-resource-name>

      <url-pattern>/*</url-pattern>
  </web-resource-collection>

<auth-constraint>
    <role-name>role1</role-name>
    <role-name>tomcat</role-name>
</auth-constraint>

</security-constraint>

<login-config>
    <auth-method>BASIC</auth-method>
    <realm-name>My Test</realm-name>
</login-config>
<web-app>

其中,<url-pattern>中指定受限的url,可以使用通配符*,通常对整个目录进行访问权限控制。
<auth-constraint>
中指定哪些角色可以访问<url-pattern>指定的url,在<role-name>中可以设置一个或多个角色名。

使用的角色名来自tomcat的配置文件${CATALINA_HOME}/conf/tomcat-users.xml

<login-config>中设置登录方式,<auth-method>的取值为BASICFORM。如果为BASIC,浏览器在需要登录时弹出一个登录窗口。如果为FORM方式,需要指定登录页面和登录失败时的提示信息显示页面。