简体   繁体   中英

Which are the advantages of the Spring Session compared with Servlet HttpSession?

  1. Which are the advantages of the Spring Session compared with Servlet HttpSession?

  2. Where should I use the Spring session?

HttpSession

Provides a way to identify a user across more than one page request or visit to a Web site and to store information about that user. The servlet container uses this interface to create a session between an HTTP client and an HTTP server. The session persists for a specified time period, across more than one connection or page request from the user. A session usually corresponds to one user, who may visit a site many times. The server can maintain a session in many ways such as using cookies or rewriting URLs.

More Info



Spring Session

Spring Session provides a transparent approach to resolving the limitations of the HTTP session. It provides central session management without being tied to container-specific solutions (eg Tomcat, Jetty etc.)...

Traditionally HTTPSession is container-specific ie the container will create a session and provide the session ID to the application. This essentially means that if our application is running on a clustering environment, each container will have its own HTTPSession management. Session information is not shared across the different servers. The session-id generated by application server 1 will be unknown to the second application server.

More Info

Spring Session provides transparent integration with HttpSession. This means that developers can switch the HttpSession implementation out with an implementation that is backed by Spring Session.

More Info

在此处输入图像描述

Spring Session do more advanced things than Tomcat HttpSession. HttpSession it's used in Servlets and Spring Session can be used in Servlets, RestApi...

Ex: Why Spring Session and HttpSession

HttpSession actually is limited to a certain web container, which means its lifecycles are maintained by a container like Tomcat/Jetty. But when you have a couple of servers (normally a reverse agency like Nginx + several server instances), each server would have its own session, so a user will have to login more than once when successive requests hit different servers (eg Server A for the 1st time and server B for the 2nd time).

Keys to address this problem include:

1.Sync session informations among all server instances or

2.Maintain a global session

Actually Spring session aims at the 2nd solution, providing a global session management framework while supporting extensions with different 3rd-party persistence libraries like Redis/JDBC/In Memory.

Theoretically syncing sessions by yourself is also ok, but hard to maintain and time-consuming.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM