Session details


1. When the client is closed, the server does not close, is the same session obtained twice?

Answer: By default, it is not the same. If two Sessionidentical, you can create an Cookieobject keys JSESSIONID, set about the maximum survival time for the Cookielasting preservation Sessionof ID, you can achieve the client closes, twice get Sessionis the same.

Let's demonstrate it below:

@WebServlet("/sessionDemo3")
public class SessionDemo3 extends HttpServlet {
    
    
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    
    
        //获取HttpSession对象
        HttpSession session = request.getSession();
        System.out.println(session);

        //期望客户端关闭之后,session也能相同
        Cookie cookie = new Cookie("JSESSIONID",session.getId());
        cookie.setMaxAge(60 * 10); //设置cookie存活时间为十分钟
        response.addCookie(cookie); //发送cookie
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    
    
        this.doPost(request, response);
    }
}

Run the server, open the browser to access this Servlet, close the browser and visit again, the console output is as follows:
Insert picture description here

2. The client does not close. After the server is closed, is the same Session obtained twice?

Answer: Not the same, but in order to ensure that data is not lost, tomcatthe following two tasks are automatically completed:

  • session的钝化: Before the server shuts down normally, sessionserialize the object to the hard disk
  • session的活化: After the server is started, the sessionfile is converted into an sessionobject in memory

This completes the data is not lost, tomcatcan help us to complete the above work, but IDEAcan not!

As for why Tomcatthis work can be done? We write two new ones Servlet, one for sessionstoring data, one for obtaining sessiondata and printing (the code is simple, no longer paste)

Then put the project outunder the generated project folder compress, packaged as to the .warfile name suffix, then put his copy Tomcatdirectory webappsdirectory:
Insert picture description here
running Tomcat, open a browser, access the send sessionof Servlet, then visit reception sessionof Servlet, You can see that the console prints hello:
Insert picture description here
OK, now we let the Tomcatnormal shutdown ( shutdown.bat), after closing, Tomcatstart to perform 钝化operations.

It Sessionserializes to:apache-tomcat-8.5.61\work\Catalina\localhost\SessionDemoUnder the folder:
Insert picture description here
restart Tomcat, that is, execute the 活化operation, the above file that saves the serial number will also be automatically deleted, and then we visit the receiving information Servlet: the
Insert picture description here
console output hello! success!

3. When will the Session be destroyed?

SessionDestroyed after the server is closed, if you open a page, the computer Sessionwill be destroyed if you do not move the computer for a certain period of time

If we expect sessionto survive longer, we can go to the tomcat\conf\web.xmlfile and find:

  <!-- ==================== 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>

For this code, 30just change that . This 30 is the default 30 minutes.

Guess you like

Origin blog.csdn.net/lesileqin/article/details/112688141