Simple session shared encapsulation

objective

The session is stored on the cache server (all kinds of cache servers can be used. This article takes memcached as an example), but for developers, they don't need to pay attention, they just need to call request Getsession () method can get the session, and then operate on the properties of the session.

Problems faced

  1. The session is not obtained from the application server, but from memcached.

  2. The session attribute is not set to the application server, but is obtained or set by operating memcached.

Solutions to problems

  1. Use an implementation class of httpservletrequestwrapper, override the getsession () method, and then use the filter to filter each request to turn the request into a requestwrapper.

  2. Use an implementation class of HttpSessionAttributeListener to override the attributeadded(), attributeremoved(), and attributereplaced() methods. When the attribute changes, you need to notify the session in memcached of the change

In addition, in order to solve the possible compatibility problems of heterogeneous systems due to lack of language, session is stored in JSON string.

The specific codes are as follows:

Wrapper class

Note: the above code is only the demo code, and the actual application needs to reconstruct the code.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>