Java – J2EE: Singleton vs saving things in a session

When should I keep an object (that is, an application wide property file) in the session instead of creating a singleton to save it? When should these methods be used?

Note: I am developing a cluster environment, if there is any difference

Solution

If it should be application - scoped, you should not store it in session scope, but in application scope When stored in session scope, you unnecessarily copy the same data for each visitor There is no need for a singleton at all. You only need to instantiate it once with the help of servletcontextlistener during server startup and store it in the application scope with servletcontext#setattribute()

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
分享
二维码
< <上一篇
下一篇>>