如果servlet类在加载后由容器创建了多少个实例?辛格尔顿? [英] How many instaces if servlet class are created by container after loading it ? Singleton?

查看:130
本文介绍了如果servlet类在加载后由容器创建了多少个实例?辛格尔顿?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果servlet类在加载后由容器创建了多少个实例?
只有一个>

How many instaces if servlet class are created by container after loading it ? is it only one >

推荐答案

Servlets 坚持单身人士模式。 Servlet类有一个公共构造函数,它已经违反了单例模式。默认情况下,servlet的创建只是 Just Create One模式。从技术上讲,容器可以根据需要创建尽可能多的容器。特别是如果servlet碰巧实现(已弃用) SingleThreadedModel 接口,以便容器可以创建这些servlet的池以提高性能。

Servlets do not adhere the Singleton pattern. Servlet classes have a public constructor which already violates the singleton pattern. The creation of servlets is by default just the Just Create One pattern. Technically, the container can create as many of them as it needs. Especially if the servlet happens to implement the (deprecared) SingleThreadedModel interface so that the container can create a pool of those servlets in order to improve performance.

但是,确实可以在多个请求之间共享同一个实例。这就是将任何请求/会话范围数据分配为servlet的实例变量非常重要的原因。它将在多个请求之间共享。

However, it is true that the same instance can be shared among multiple requests. That's why it's very important to not assign any request/session scoped data as an instance variable of the servlet. It would be shared among multiple requests.

Servlets本身遵循模板方法模式。这与创作无关,它只是一种行为模式。

Servlets at its own adheres the Template Method pattern by the way. This is not related to creation, it is just a behavioral pattern.

  • How do servlets work? Instantiation, sessions, shared variables and multithreading

这篇关于如果servlet类在加载后由容器创建了多少个实例?辛格尔顿?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆