program story

SessionTimeout : web.xml 대 session.maxInactiveInterval ()

inputbox 2020. 12. 11. 08:08
반응형

SessionTimeout : web.xml 대 session.maxInactiveInterval ()


Java 에서 HttpSession 시간 초과를 시도하고 있습니다. 내 컨테이너는 WebLogic입니다.

현재 web.xml 파일에 다음과 같이 세션 시간 제한이 설정되어 있습니다.

<session-config>
    <session-timeout>15</session-timeout>
</session-config>

이제 활동에 관계없이 사용 15 분 내에 세션 (또는 모든 세션입니까?)이 종료된다는 말을 들었습니다.

이 접근 방식이 올바른지 또는 프로그래밍 방식으로 비활성 시간 제한을 설정해야하는지 궁금합니다.

session.setMaxInactiveInterval(15 * 60); //15 minutes

모든 세션을 15 분에 중단하는 것이 아니라 15 분 동안 비활성 상태 인 세션 만 중단하고 싶습니다.

이 방법은 동일합니까? web.xml 구성을 선호해야합니까 ?


이제 활동에 관계없이 사용 15 분 내에 세션 (또는 모든 세션입니까?)이 종료된다는 말을 들었습니다 .

이것은 잘못된 것 입니다. 연결된 클라이언트 (웹 브라우저)가 15 분 이상 웹 사이트에 액세스하지 않으면 세션이 종료됩니다. 이 활동은 처음에 예상했던대로 정확하게 계산되며이를 해결하려는 시도를 볼 수 있습니다.

그런데 HttpSession#setMaxInactiveInterval()여기서는 많이 변하지 않습니다. 런타임 중에 프로그래밍 방식으로 변경 / 설정할 수있는 유일한 차이점을 제외하고 <session-timeout>에서 와 정확히 동일 web.xml합니다. 그런데 변경 사항은 현재 세션 인스턴스에만 영향을 미치며 전역 적으로는 영향을주지 않습니다 (그렇지 않으면 static메서드 였을 것입니다 ).


직접 플레이하고 경험하려면 <session-timeout>1 분 으로 설정 하고 HttpSessionListener다음과 같이 만드세요 .

@WebListener
public class HttpSessionChecker implements HttpSessionListener {

    public void sessionCreated(HttpSessionEvent event) {
        System.out.printf("Session ID %s created at %s%n", event.getSession().getId(), new Date());
    }

    public void sessionDestroyed(HttpSessionEvent event) {
        System.out.printf("Session ID %s destroyed at %s%n", event.getSession().getId(), new Date());
    }

}

(아직 Servlet 3.0을 사용하지 않아를 사용할 수없는 경우 다음과 같이 @WebListener등록하십시오web.xml .)

<listener>
    <listener-class>com.example.HttpSessionChecker</listener-class>
</listener>

servletcontainer는 정확히 시간 초과 값 이후에 세션을 즉시 파괴하지 않습니다 . 특정 간격 (예 :로드 및 servletcontainer make / type에 따라 5 ~ 15 분)으로 실행되는 백그라운드 작업입니다. 따라서 destroyed정확히 1 분 동안 활동 이 없으면 콘솔에 줄 이 표시되지 않더라도 놀라지 마십시오 . 그러나 시간이 초과되었지만 아직 파괴되지 않은 세션에서 HTTP 요청을 실행하면 즉시 파괴됩니다.

또한보십시오:


이제 활동에 관계없이 사용 15 분 내에 세션 (또는 모든 세션입니까?)이 종료된다는 말을 들었습니다.

아니요, 사실이 아닙니다. session-timeout활동의 경우 세션 당 시간 제한을 구성합니다.

이 방법은 동일합니까? web.xml 구성을 선호해야합니까?

web.xml의 설정은 전역 적이며 주어진 컨텍스트의 모든 세션에 적용됩니다. 프로그래밍 방식으로 특정 세션에 대해이를 변경할 수 있습니다.


아래 의사 코드에서 Seession 제한 시간을 확인하십시오.

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
    xmlns="http://java.sun.com/xml/ns/j2ee"`enter code here`
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instanenter code herece"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    <display-name>AccountWeb</display-name>

    <listener>
        <description>[Re]configures log4j</description>
        <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
    </listener>

    <context-param>
      <description>How often to check for changes in configfile (ms)</description>
      <param-name>log4jRefreshInterval</param-name>
      <param-value>60000</param-value>
    </context-param>
    <context-param>
      <description>Avoid setting system property as there might be several apps in same VM</description>
      <param-name>log4jExposeWebAppRoot</param-name>
      <param-value>false</param-value>
    </context-param>

    <listener>
        <description>The listener that will start Account</description>
        <display-name>AccountInitialiser</display-name>
        <listener-class>com.te.account.AccountInitializer</listener-class>
    </listener>

    <servlet>
        <display-name>Apache-Axis Servlet</display-name>
        <servlet-name>AxisServlet</servlet-name>
        <servlet-class>org.apache.axis.transport.http.AxisServlet</servlet-class>
          <load-on-startup>200</load-on-startup>
    </servlet>

    <servlet>
        <display-name>Axis Admin Servlet</display-name>
        <servlet-name>AdminServlet</servlet-name>
        <servlet-class>org.apache.axis.transport.http.AdminServlet</servlet-class>
        <load-on-startup>100</load-on-startup>
    </servlet>


    <servlet-mapping>
        <servlet-name>AxisServlet</servlet-name>
        <url-pattern>/servlet/AxisServlet</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>AxisServlet</servlet-name>
        <url-pattern>*.jws</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>AxisServlet</servlet-name>
        <url-pattern>/services/*</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>AdminServlet</servlet-name>
        <url-pattern>/servlet/AdminServlet</url-pattern>
    </servlet-mapping>

    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
    </welcome-file-list>
<!--    <resource-ref>
        <description>The queue used to publish logging events</description>
        <res-ref-name>jms/LoggingAppenderQueue</res-ref-name>
        <res-type>javax.jms.Queue</res-type>
        <res-auth>Container</res-auth>
        <res-sharing-scope>Shareable</res-sharing-scope>
    </resource-ref>
    <resource-ref>
        <description>The connection factory for the logging appender queue</description>
        <res-ref-name>jms/LoggingAppenderQueueConnFactory</res-ref-name>
        <res-type>javax.jms.QueueConnectionFactory</res-type>
        <res-auth>Container</res-auth>
        <res-sharing-scope>Shareable</res-sharing-scope>
    </resource-ref> -->
    <resource-ref>
        <description>
        </description>
        <res-ref-name>jdbc/AccountDB</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
        <res-auth>Container</res-auth>
        <res-sharing-scope>Shareable</res-sharing-scope>
    </resource-ref>
</web-app>

참고 URL : https://stackoverflow.com/questions/3118968/sessiontimeout-web-xml-vs-session-maxinactiveinterval

반응형