Article Directory
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 Session
identical, you can create an Cookie
object keys JSESSIONID
, set about the maximum survival time for the Cookie
lasting preservation Session
of ID
, you can achieve the client closes, twice get Session
is 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:
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, tomcat
the following two tasks are automatically completed:
session的钝化
: Before the server shuts down normally,session
serialize the object to the hard disksession的活化
: After the server is started, thesession
file is converted into ansession
object in memory
This completes the data is not lost, tomcat
can help us to complete the above work, but IDEA
can not!
As for why Tomcat
this work can be done? We write two new ones Servlet
, one for session
storing data, one for obtaining session
data and printing (the code is simple, no longer paste)
Then put the project out
under the generated project folder compress, packaged as to the .war
file name suffix, then put his copy Tomcat
directory webapps
directory:
running Tomcat
, open a browser, access the send session
of Servlet
, then visit reception session
of Servlet
, You can see that the console prints hello
:
OK, now we let the Tomcat
normal shutdown ( shutdown.bat
), after closing, Tomcat
start to perform 钝化
operations.
It Session
serializes to:apache-tomcat-8.5.61\work\Catalina\localhost\SessionDemoUnder the folder:
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
console output hello
! success!
3. When will the Session be destroyed?
Session
Destroyed after the server is closed, if you open a page, the computer Session
will be destroyed if you do not move the computer for a certain period of time
If we expect session
to survive longer, we can go to the tomcat\conf\web.xml
file 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, 30
just change that . This 30 is the default 30 minutes.