当前位置:网站首页>What are the ways to destroy session?

What are the ways to destroy session?

2020-12-07 10:20:20 A paper umbrella

problem :Session What are the ways to destroy ?

Nianan is in b Stand and tell me ~

Session, As we cannot do without the backstage Technology , It appears mainly to solve Http Agreed No state characteristic , Used to solve the storage problem of user state , And often for storage, there will be a time problem , Let's take a look at the ways in which it is destroyed .

Methods of destruction

  • The default time is due
  • Set your own expiration date
  • Lapse immediately
  • Close the browser
  • Shut down the server

Case practice

The default time is due

When the client first requests servlet And the operating session when ,session Object to generate , With Tomcat For example ,Tomcat in session The default lifetime is 30min, That is, when you don't operate the interface , Once there's an operation ,session It's going to be timed again . that session Can I change the default time of ? The answer is yes . Can be in Tomcat Medium web.xml Make changes in the document . Here's the picture :

Set your own expiration date

Of course, in addition to the above modifications , We can also set our own program session Life cycle of , adopt session.setMaxInactiveInterval(int); To set session The maximum inactivity time of , The unit is in seconds .

HttpSession session = req.getSession();
session.setMaxInactiveInterval(5); 

Of course we can pass it getMaxInactiveInterval(); Method to view the current Session The maximum inactivity time of the object .

Lapse immediately

Or we can go through session.invalidate(); Ways to make session Lapse immediately .

session.invalidate(); 

Nianan is in b Stand and tell me ~

Close the browser

session The bottom layer of dependence cookie Realization , Because different users need to distinguish which one to use when accessing the server session, So when a user visits the server for the first time, he often puts a session id adopt cookie Store to the client , And it's time to cookie The valid time for closing the browser , thus session When the browser is closed, it will be invalid ( Because no session id And then it corresponds to ). Here's the picture , Close and then open , Reassigned browser to session id.

image-20200708195027943

It's important to note that this is just cookie It doesn't work , If you visit again, the server will treat you as a new user , I created another one for you session, Did not put the previous session Destruction of objects .

Shut down the server

When the server is shut down abnormally ,session The destruction ; When the server is shut down normally ,session Will be serialize To disk , In the workspace work In the catalog SESSION.ser In file , If the object is saved in session in , The server will serialize the object to the hard disk when it is shut down , This object must be implemented Serializable Interface , The next time you start the service , Auto load to memory . Here's the picture , After normal closing, you can see that there is one more in the folder SESSIONS.ser file , Start the server again and the file will disappear .

Expand ~Cookie Destruction of

In addition to seeing from the picture Cookie The name and content of , There's another message we need to care about , Due time , The expiration time is used to specify the cookie When does it fail . The default is that the current browser is disabled when it is closed . We can set it manually cookie Effective time of ( Calculated by the expiration time ), adopt setMaxAge(int expiry); Method setting cookie Maximum effective time of , With second In units of .

  • Greater than 0 The integer of , Represents the number of seconds stored ; If negative , It means that the... Is not stored cookie; if 0, Delete the cookie.
  • Negtive integer :cookie Of maxAge The default value of the property is -1, It means only in browser memory , Once the browser window is closed , that cookie Will disappear .
  • Positive integer : Express cookie Object can survive for a specified number of seconds . When life is greater than 0 when , The browser will Cookie Save to hard disk , Even close the browser , Even restart the client computer ,cookie It will survive for a corresponding period of time .
    e Object can survive for a specified number of seconds . When life is greater than 0 when , The browser will Cookie Save to hard disk , Even close the browser , Even restart the client computer ,cookie It will survive for a corresponding period of time .
  • zero :cookie Life is equal to 0 It's a special value , It said cookie Be void ! in other words , If the original browser has saved this Cookie, So you can go through Cookie Of setMaxAge(0) To delete this Cookie. Whether it's in browser memory , Or it will be deleted on the client's hard disk Cookie.

Nianan is in b Stand and tell me ~

版权声明
本文为[A paper umbrella]所创,转载请带上原文链接,感谢
https://chowdera.com/2020/12/20201207101726069a.html