Home  >  Article  >  Java  >  What is the life cycle of session?

What is the life cycle of session?

(*-*)浩
(*-*)浩Original
2019-05-06 13:59:5724524browse

Session is difficult to directly translate into Chinese, and is generally translated into the time domain. In computer terminology, Session refers to the time interval between an end user communicating with an interactive system.

usually refers to the time elapsed between registering to enter the system and logging out of the system. And there may be some room for maneuver if needed.

Recommended course: Java Tutorial.

What is the life cycle of session?

Session life cycle

Session is stored in the server's memory (for high-speed access).

When does Session take effect?

Session is created when the user accesses the server for the first time. It should be noted that Session will only be created when accessing JSP, Servlet and other programs. Access HTML, IMAGE, etc. State resources will not create a Session. You can call request.getSession(true) to force a Session.

When does the Session expire?

1. The server will clear the Session that has been inactive for a long time from the server memory, and the Session will expire at this time. The default expiration time of Session in Tomcat is 20 minutes.

2. Call the invalidate method of Session.

HttpSession session = request.getSession();
session.invalidate();//注销该request的所有session

3. When does the expiration time of session start to be calculated? Does it start counting as soon as you log in or does it start counting when you stop being active?

Answer: It is calculated from the time when the session is inactive. If the session is always active, the session will never expire.

The timer starts when the Session is not accessed; once the Session is accessed, the timer is cleared to 0;

4. Set the session expiration time

a)

<session-config>
    <session-timeout>30</session-timeout>
</session-config>

in web.xml b) Manually set

session.setMaxInactiveInterval(30 * 60);//设置单位为秒,设置为-1永不过期

in the program c) Tomcat can also modify the session expiration time. When defining context in server.xml, use the following Definition:

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

[Note]: If you want to remove a specific value in the Session, you can call the removeAttribute method, but the SessionID in the request remains unchanged, but the value has changed. Session is still the original Session. ↓↓↓↓↓↓↓↓

request.getSession().removeAttribute("xxx");

The above is the detailed content of What is the life cycle of session?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn