Does each request access the same servlet object?

前端 未结 2 1999
渐次进展
渐次进展 2020-12-30 01:04

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 ?

2条回答
  •  北海茫月
    2020-12-30 01:59

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

    From the Servlet 3.0 specification:

    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.

    ...

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

提交回复
热议问题