java servlet 是单例吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11820840/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is servlet the singleton?
提问by Adam Lee
Reading some book that said the servlet is singleton from the container side. Is this true?
阅读一些书,上面说 servlet 从容器方面是单例的。这是真的?
However even it is a singleton, we need to handle data synchronization etc
然而即使是单例,我们也需要处理数据同步等
采纳答案by user207421
No. You can instantiate the same servlet class many times under different servlet names and URLs in the same web container and indeed in the same web-app.
不可以。您可以在同一个 web 容器中,甚至在同一个 web 应用程序中,在不同的 servlet 名称和 URL 下多次实例化同一个 servlet 类。
回答by Konrad Reiche
Looking at the definition of the Singleton Pattern as defined in the Cunningham & Cunningham, Inc. Wiki
查看Cunningham & Cunningham, Inc. Wiki 中定义的单例模式的定义
Ensure a class has only one instance, and provide a global point of access to it.
确保一个类只有一个实例,并提供对它的全局访问点。
I would say, no. From the perspective of the container one servlet object is accepted and managed including the creation of a ServletContext, but it does not prevent that there is not more than one instance of the servlet.
我会说,不。从容器的角度来看,一个 servlet 对象被接受和管理,包括ServletContext的创建,但这并不阻止不超过一个 servlet 实例。
Regarding such issues I think it's best to look into the corresponding contract, which is in case of servlets defined in the Java Servlet Specification. They have addressed the number of instances of a servlet.
关于此类问题,我认为最好查看相应的合同,这是在Java Servlet Specification 中定义的 servlet 的情况下。他们已经解决了 servlet 实例的数量。
2.2 Number of Instances
The servlet declaration which is either via the annotation as described in Chapter 8, “Annotations and pluggability” or part of the deployment descriptor of the Web application containing the servlet, as described in Chapter 14, “Deployment Descriptor”, controls how the servlet container provides instances of the servlet. For a servlet not hosted in a distributed environment (the default), the servlet container must use only one instance per servlet declaration. However, for a servlet implementing the SingleThreadModel interface, the servlet container may instantiate multiple instances to handle a heavy request load and serialize requests to a particular instance.
In the case where a servlet was deployed as part of an application marked in the deployment descriptor as distributable, a container may have only one instance per servlet declaration per Java Virtual Machine (JVM?). However, if the servlet in a distributable application implements the SingleThreadModel interface, the container may instantiate multiple instances of that servlet in each JVM of the container.
2.2 实例数
servlet 声明通过第 8 章“注释和可插入性”中描述的注解或包含 servlet 的 Web 应用程序的部署描述符的一部分(如第 14 章“部署描述符”中描述)控制 servlet 容器提供 servlet 的实例。对于不在分布式环境(默认)中托管的 servlet,servlet 容器必须在每个 servlet 声明中仅使用一个实例。但是,对于实现 SingleThreadModel 接口的 servlet,servlet 容器可能会实例化多个实例以处理繁重的请求负载并将请求序列化到特定实例。
如果 servlet 被部署为在部署描述符中标记为可分发的应用程序的一部分,那么每个 Java 虚拟机 (JVM?) 的每个 servlet 声明容器可能只有一个实例。但是,如果可分发应用程序中的 servlet 实现 SingleThreadModel 接口,则容器可能会在容器的每个 JVM 中实例化该 servlet 的多个实例。
It only specifies that the container must only use one instance (in the former case) and as EJPhas pointed out in the comment:
它只指定容器只能使用一个实例(在前一种情况下),正如EJP在评论中指出的那样:
There is nothing in the Servlet Specification that prevents you from re-instantiating the same servlet class under a different name in the same web-app. Ergo, not a singleton.
Servlet 规范中没有任何内容可以阻止您在同一个 web 应用程序中以不同的名称重新实例化同一个 servlet 类。因此,不是单身人士。
ReferenceJava Servlet Specification 3.0 MR(p.6-7)
参考Java Servlet 规范 3.0 MR(第 6-7 页)
回答by SKC...
No, Servlet is not a Singleton. It may create 2nd Object , depends on the incoming request and the Container behavior.
不,Servlet 不是单例。它可能会创建 2nd Object ,这取决于传入的请求和 Container 行为。