Java Setting session Expiration Time implementation method _java

Source: Internet
Author: User

This article describes the Java set session expiration time to achieve the method to share for everyone to reference. The implementation methods are as follows:

1, Timeout in the deployment descriptor (Web.xml)
In minutes

Copy Code code as follows:
<web-app ...>
<session-config>
<session-timeout>20</session-timeout>
</session-config>
</web-app>

This setting takes effect for the entire Web application. When the client does not initiate a request within 20 minutes, the container kills the session.

2. Timeout with Setmaxinactiveinterval ()
Specifies, in seconds, the expiration time of a specific session by encoding. For example:

Copy Code code as follows:
HttpSession session = Request.getsession ();
Session.setmaxinactiveinterval (20*60);

The above setting is only in session which called "Setmaxinactiveinterval ()" method, and session would be kill by C Ontainer if client doesn ' t make no request after minutes.

Thoughts .....
This is a bit confusing, the value in Deployment Descriptor (Web.xml) are in "minute", but the Setmaxinactiveinterval () Me Thod is accept the ' value in ' second '. Both functions should synchronize it in future release

3, defined in the program, the unit is seconds, set to-1 means never expire, sample code is:

Copy Code code as follows:
Session.setmaxinactiveinterval (30*60);

Session settings have the effect of priority is, first after the program configuration, first local after the whole.

I hope this article will help you with your Java programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.