gpt4 book ai didi

java - servlet 容器是否应该为每个传入请求创建新的 javax.servlet.http.HttpServlet 实例?

转载 作者:搜寻专家 更新时间:2023-10-31 08:29:58 24 4
gpt4 key购买 nike

我有一个类 public class GAE_SERVLETREQUESTServlet extends HttpServlet {

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

我正在调查一个有趣的问题,似乎在 GAE_SERVLETREQUESTServlet 实例上创建的 map 在请求之间保持状态。

最佳答案

对于一般情况——非分布式、多线程,保证只有一个servlet实例。来自 Servlet 3.0 规范:

2.1 Request Handling Methods

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

关于java - servlet 容器是否应该为每个传入请求创建新的 javax.servlet.http.HttpServlet 实例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4042930/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com