Session Technology_Session_Details

  Session details:

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

     ①Not by default.

    ②If the requirements are the same, you can create a Cookie, the key is JSESSIONID, set the maximum survival time, and make the cookie persistent.

          Cookie ck = new Cookie("JSESSIONID",session.getId());

         ck.setMaxAge(60*60);

         response.addCookie(ck);

package jason.lh.session;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;

import java.io.IOException;


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

        //期望客户端关闭后,session也能相同
        Cookie ck = new Cookie("JSESSIONID",session.getId());
        ck.setMaxAge(60*60);
        response.addCookie(ck);


    }

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

 

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

  Not the same, but make sure that the data will not be lost

(In this case, data may be lost. For example, adding items to the shopping cart has not yet been settled. If the server is restarted, data may be lost)

 

Passivation of session:

Before the server shuts down normally, serialize the session object to the hard disk

Activation of session:

After the server is started, convert the session file into a session object in memory.

 

##(The tomcat server has already completed the passivation and activation for us, we don’t need to think about it

It can’t be done in idea, it can be passivated, but it can’t be activated)-Operation (20_Session Technology_Session_Detail 2)

【Local Hard Disk Operation】

 

3. The expiration time of the session?

   ①The server is shut down

   ②The session object calls invalidate().

   ③The default expiration time of session is 30 minutes.

  Under D:\apache-tomcat-9.0.22\conf\web.xml

  Configuration modification

     <session-config>

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

    </session-config>

 

4. The characteristics of session

①session is used to store the data of multiple requests for a session, and it exists on the server side

②Session can store any type and size of data

Guess you like

Origin blog.csdn.net/Jason_LH1024/article/details/102891637