JavaWeb session expiration time setting method

The session expiration time setting method is as follows:

1、 Java code <-- Priority is highest -- >

2、 Web xml

3、 Web server resin conf,tomcat,

Priority: 1 > 3 > 2

Generally, the session will not fail after Tomcat is restarted. After the browser is closed, the session will fail

In general systems, you may also need to do some operations after session failure:

(1) Control the number of users. When the session fails, the number of users of the system will be reduced by one. Control the number of users within a certain range to ensure the performance of the system.

(2) Control a user to log in multiple times. When the session is valid, if the same user logs in, he will be prompted to log in. When the session fails, he can log in directly without prompting.

So how to perform a series of operations after the session fails?

The listener is needed here, that is, when the session fails for various reasons, the listener can listen, and then execute the program defined in the listener.

The listener class is HttpSessionListener, which has two methods, sessioncreated and sessiondestroyed

You can inherit this class and implement it separately.

Sessioncreated refers to the method executed when a session is created

Sessiondestroyed refers to the method executed when a session fails

As follows:

Put this listener on the web Just declare in XML:

The above is a simple method of using session to monitor the number of users. In the actual process, it may be much more complicated than this.

For example, we need to implement two interfaces, servletcontextlistener and httpsessionlister, and rewrite their methods.

The above is the Java Web session expiration time setting method introduced by Xiaobian. I hope it will be helpful to you. If you have any questions, please leave me a message, and Xiaobian will reply to you in time. Thank you very much for your support for the programming tips website!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>