每个请求是否访问相同的 servlet 对象? [英] Does each request access the same servlet object?

查看:21
本文介绍了每个请求是否访问相同的 servlet 对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每个 HTTP 请求是否访问相同的 servlet 对象但在不同的线程中?或者它是否创建了一个新线程和新的 Servlet 实例?

Does each HTTP request access the same servlet object but in a different thread? or does it create a new thread and new Servlet Instance ?

推荐答案

如果您的 servlet 没有实现 SingleThreadModel,容器将使用相同的 servlet 实例.否则无法保证命中相同的 Servlet 对象.如果认为有必要,容器可以自由创建更多 servlet 实例.但是请求来自不同的线程,不一定是新创建的(如 Sanjay 提到的).

The container will use the same servlet instance if your servlet don't implement SingleThreadModel. Otherwise there is no guarantee that the same Servlet object is hit. The container is free to create more servlet instances if it considers necessary. But the requests comes on different threads, not necessarily newly created (as Sanjay mentioned).

来自 Servlet 3.0 规范:

From the Servlet 3.0 specification:

对于不在分布式环境中托管的 servlet(默认),servlet每个 servlet 声明容器必须仅使用一个实例.但是,对于 servlet实现 SingleThreadModel 接口,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.

...

Web 容器通常通过以下方式处理对同一个 servlet 的并发请求在不同线程上并发执行服务方法.

Generally the Web container handles concurrent requests to the same servlet by concurrent execution of the service method on different threads.

这篇关于每个请求是否访问相同的 servlet 对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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