Session session (super detailed)

Posted by buildakicker on Fri, 14 Jan 2022 21:32:03 +0100

1, What is a Session

  1. Session is an interface (HttpSession).
  2. A Session is a Session. It is a technology used to maintain an association between a client and a server.
  3. Each client has its own Session.
  4. In the Session session, we often use it to save the information after the user logs in.

2, How to create a Session and get (id number, new or not)

How to create and get a Session. Their API s are the same.

request.getSession()

  1. The first call is to create a Session
  2. After that, you can get the Session session object that you created earlier.

isNew();--------- Judge whether it is newly created (New)

  1. true indicates that it has just been created
  2. false indicates created before getting

Each session has an ID number. That is, the ID value. And this ID is unique.

getId() -------- get the Session id value of the Session

3, Session domain data access

//Save number to Session
req.getSession().setAttribute("key1", "value1");

// Get the data in the Session domain
Object attribute = req.getSession().getAttribute("key1");

4, Session lifecycle control

Public void setmaxinactivitinterval (int interval) -- sets the timeout of the Session (in seconds). If it exceeds the specified duration, the Session will be destroyed.

  • When the value is positive, set the timeout length of the Session.
  • A negative number means never timeout (rarely used)

Public int getmaxinactivival() -- get the timeout of Session

public void invalidate() -- make the current Session timeout invalid immediately.

The default timeout for Session is 30 minutes.

Because in the configuration file of Tomcat server, web XML has the following configuration by default, which means that all Session timeout configurations under the current Tomcat server are configured. The default duration is 30 minutes.  

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

if You want your web project to have a default Session timeout of other durations. You can on your own web Do the same configuration as above in the XML configuration file. You can modify the default timeout of all sessions of your web project.

<!--Represents the current web Engineering. All created Session The default timeout is 20 minutes-->
<session-config>
<session-timeout>20</session-timeout>
</session-config>

If you want to modify only the timeout duration of individual sessions. You can use the above API.

Setmaxinactivitinterval (int interval) to set separately.

protected void life3(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // Get the Session object first
        HttpSession session = req.getSession();
        // Set the timeout after 3 seconds for the current session
        session.setMaxInactiveInterval(3);

        resp.getWriter().write("current Session Has been set to timeout after 3 seconds");
    }

5, Session} timeout control

Example code:

Set the timeout after 3 seconds for the current session

protected void life3(HttpServletRequest req, HttpServletResponse resp) throws ServletException,
IOException {
// Get the Session object first
HttpSession session = req.getSession();
// Set the timeout after 3 seconds for the current session
session.setMaxInactiveInterval(3);
resp.getWriter().write("current Session Has been set to timeout after 3 seconds");
}

The Session timed out immediately. Example:

protected void deleteNow(HttpServletRequest req, HttpServletResponse resp) throws ServletException,
IOException {
// Get the Session object first
HttpSession session = req.getSession();
// Let the Session timeout immediately
session.invalidate();
resp.getWriter().write("Session Has been set to timeout (invalid)");
}

6, Technical insider of the association between browser and Session

Session technology, the bottom layer is actually based on cookies

Topics: Database server DBA