开发者

Where to store a Servlet's singletons?

If my Servlet class uses a singleton (such as a manager class), where should i开发者_如何学Pythont be stored? The servlet itself, or in the ServletContext?

Can a servlet container create more than one instance of my Servlet class to handle requests?


It is certain that there will be only one instance of a Servlet. But still, it's better to store it in the ServletContext. Thus it will be accessible from other servlets as well.


You can store it basically anywhere you want; in a session, application context or as a field of that servlet itself. Just be sure to make it immutable since you're dealing with a multithreaded environment and your servlet will be called multiple times at once.

Edit: as Bozho pointed out, using a session might not be the best option, so you should evaluate your needs before putting your singleton in a session.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜