servlet容器是否应为每个传入请求创建新的javax.servlet.http.HttpServlet实例? [英] Should a servlet container create new javax.servlet.http.HttpServlet instance for each incoming request?

查看:153
本文介绍了servlet容器是否应为每个传入请求创建新的javax.servlet.http.HttpServlet实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类公共类GAE_SERVLETREQUESTServlet扩展HttpServlet {

不确定规范中有关回收的内容HTTPServlet:servlet容器是应该在每个传入请求上创建此类的新实例,还是实现可以在请求之间重用类?

Not sure what the spec says about recycling of the HTTPServlet: Should the servlet container create new instance of this class on each incoming request or can the implementation reuse classes between requests?

我正在调查一个有趣的问题似乎在GAE_SERVLETREQUESTServlet实例上创建的Map维护请求之间的状态。

I'm investigating a funny issue where it seems that a Map created on the GAE_SERVLETREQUESTServlet instance maintains state between requests.

推荐答案

对于一般情况 - 非分布式,多thread,保证只有一个servlet实例。从Servlet 3.0规范:

For the general case - non-distributed, multi-threaded, it is guaranteed that there will be only one instance of the servlet. From the Servlet 3.0 specification:


2.1请求处理方法

基本的Servlet接口定义了一种处理客户端请求的服务方法。
为servlet容器路由到servlet的实例
的每个请求调用此方法。
处理对Web应用程序的并发请求通常需要
Web Developer设计servlet,它可以处理在特定时间内在
内执行服务方法的多个线程。
通常,Web容器通过
在不同线程上并发执行服务方法来处理对同一servlet的并发请求。

The basic Servlet interface defines a service method for handling client requests. This method is called for each request that the servlet container routes to an instance of a servlet. The handling of concurrent requests to a Web application generally requires that the Web Developer design servlets that can deal with multiple threads executing within the service method at a particular time. Generally the Web container handles concurrent requests to the same servlet by concurrent execution of the service method on different threads.

2.2实例数

通过注释的servlet声明如第8章所述,
注释和可插入性或包含servlet的Web
应用程序的部署描述符的一部分,如第14章部署
描述符中所述,控制如何servlet容器提供servlet的实例。
对于未托管在分布式环境中的servlet(缺省值),servlet
容器每个servlet声明只能使用一个实例
。但是,对于实现SingleThreadModel接口的servlet
,servlet容器可以
实例化多个实例以处理繁重的请求加载并将请求
序列化到特定实例。

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.

如果将servlet部署为
部署描述符中标记为可分发的应用程序的一部分,则每个Java Virtual的每个
servlet声明的容器可能只有一个实例机器(JVM™)1。但是,如果
可分发应用程序中的servlet实现SingleThreadModel接口,则容器
可以在容器的每个JVM中实例化该servlet的多个实例。

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™)1. 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.

这篇关于servlet容器是否应为每个传入请求创建新的javax.servlet.http.HttpServlet实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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