Understand session in java

  • 2020-11-20 06:06:25
  • OfStack

Take a look at one of the best explanations of session:

session is a session that stores user information without closing the browser, acting as a temporary container for these temporary items. For example, if the login saves user information from one webpage to another, the user information can be saved with session and the website shopping cart can be realized with session

Why Session

To fill the limitation of Http agreement, when a user to access a page, the server returns over the request (such as after you visit a web page, the page will be the page content, interface UI presented to you), even over, is disconnected, the server is no longer to track the task status of the client (browser), so Http every request is independent, discontinuous, Http also referred to as a stateless protocol. So if we want to manipulate some of the user's own data in a situation, or in a particular process, it can be very cumbersome and even dangerous. For example, you can interact with the server through url passing parameters and implement operations.

The appearance of Session(session) solves this problem. Session is created on the server and destroyed by the server after a fixed time. During this time, the session between the client and the server will be maintained, and the client will use the Session information on the server to find or manipulate some data.

How do I use Session

Java Api gives us just one way to get session relevant to the current session:


HttpSession session = request.getSession();
// or 
HttpSession session = request.getSession(boolean);

Setting:


session.setAttribute("key",  The value object );

Get the value:


 Object type  obj = ( Object type )session.getAttribute("key");
// Such as 
String name = (String)session.getAttribute("key");

Delete session specified property key:


session.removeAttribute("key");

Clear all session to invalidate the current session completely:


session.invalidate();

session timeout period Settings

Tomcat Installation location conf/ ES53en. xml:


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

30 minutes (

Tomcat Installation location conf/ ES62en. xml:


<Context path="/test" docBase="/test" 
  defaultSessionTimeOut="3600" isWARExpanded="true" 
  isWARValidated="false" isInvokerEnabled="true" 
  isWorkDirPersistent="false"/>

The unit is in seconds

3. Java code setting:


HttpSession session = request.getSession();
session.setMaxInactiveInterval(1200);

Zero:

Other Instructions:

1. Expiration of session:

1 > . Client browser is closed:

2 > .session session expired;

3 > The client session called.invalidate ();

2. Whether the browser is closed and session is still there;

When the client browser is closed, session will still exist in the server for a fixed time, but when the browser is opened again, a new session will be generated. The browser matches the server session by generating the sessionid attribute. Although the last session is still there, I cannot visit it.

3. < % @ page session="false" % > What's going on ? :

session is not currently available, but the page session can be created.

4. When was session created:

Created when the program calls ES117en.getSession (true); If the page is not used < %@ page session="false"% > When the jsp page is compiled to Servlet, HttpSession session = ES127en.getSession (true) will be automatically added;

conclusion

That's all you need to know about session in java. If there is any deficiency, please let me know. Thank you for your support!


Related articles: