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

查看:77
本文介绍了每个请求是否都访问相同的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不在,容器将使用相同的servlet实例实现 SingleThreadModel
否则无法保证匹配相同的 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声明必须只使用一个实例。但是,对于实现SingleThreadModel接口的servlet
,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天全站免登陆