What is a Session? 1.Session is a Session, an object representing the connection between a client and a server. 2.Session is a domain object. 3.Session is often used to store user data.

How to create a Session and get (id number, whether new) by calling a method request.getSession(). The first call is to create the Session object and return it and then the next call is to get the Session object.

IsNew () returns whether the current Session was just created, and returns true to indicate that the Session was just created. Return false to obtain.

Each Session has its own unique ID.

Session Domain data access setAttribute Saves data getAttribute obtains data

protected void setAttribute(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String username = request.getParameter("username"); HttpSession session = request.getSession(); session.setAttribute("key1", username); Response.getwriter ().write(" I have saved the requested parameter [" + username + "] to the Session field "); } protected void getAttribute(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Object attribute = request.getSession().getAttribute("key1"); Response.getwriter ().write(" The data you just saved is: "+ attribute); }Copy the code

Session life cycle Control On the Tomcat server, the default Session life time is 30 minutes. In the Tomcat configuration file web.xml, there is the following configuration: The following configuration determines all web projects on the Tomcat server, and all Session objects created. The default timeout period is 30 minutes.

<! -- ==================== Default Session Configuration ================= --> <! -- You can set the default session timeout (in minutes) for all newly --> <! -- created sessions by modifying the value below. --> <session-config> <session-timeout>30</session-timeout> </session-config>Copy the code

We can also configure a separate timeout for our own Web projects. Just do the same configuration in the web.xml configuration file in your own Web project. Set timeout for all sessions in your web project to 20 minutes.

<! <session-config> <session-timeout>20</session-timeout> </session-config>Copy the code

The configuration of the above configuration file takes effect for the Tomcat server or all sessions created by the Web project. If you want to set up a single Session, you can use the API to set it up separately

setMaxInactiveInterval( time ) ; If the value is positive, it means the Session will timeout after the specified number of seconds. If the value is negative, it means the Session will never timeout (rarely used).

Session timeout refers to the interval between two requests from the client and the server. If there are no requests, the Session times out.

Invalidate () : enables the current session to be timed out immediately

protected void deleteNow(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { HttpSession session = request.getSession(); session.invalidate(); // End current Session Response.getwriter ().write(" current Session has timed out "); } protected void life3(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { HttpSession httpSession = request.getSession(); httpSession.setMaxInactiveInterval(3); // indicates that the session will time out after 3 seconds. Response.getwriter ().write(" the current Session timeout has been set to 3 seconds "); } protected void defaultLife(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { HttpSession session = request.getSession(); // Get the maximum request interval between client and server. // The timeout period. Position for the second int maxInactiveInterval = session. GetMaxInactiveInterval (); Response.getwriter ().write(" Default timeout: "+ maxInactiveInterval); }Copy the code

Technical insider on the association between browsers and sessions