Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Is Servlet single or multiple?

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "whether Servlet is single or multiple". In daily operation, I believe many people have doubts about whether Servlet is single or multiple. The editor consulted all kinds of data and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the question of "Servlet is single or multiple". Next, please follow the editor to study!

In the Servlet specification, Servlet singletons and multiple cases are defined as follows:

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.

The above specification mentions

If a Servlet is not deployed in a distributed environment, a Servlet declared in the general web.xml corresponds to only one instance.

If a Servlet implements the SingleThreadModel interface, multiple instances will be initialized. How many examples are there? there are no details here.

Let's find out what the specific reference implementation looks like from the source code of Tomcat. The following code comes from the StandardWrapper class of Tomcat. I deleted the unrelated parts.

Public Servlet allocate () throws ServletException {

Boolean newInstance = false

If (! singleThreadModel) {

/ / Load and initialize our instance if necessary

If (instance = = null) {

Synchronized (this) {

If (instance = = null) {

Try {

Instance = loadServlet ()

} catch (ServletException e) {}

If (singleThreadModel) {

If (newInstance) {

Synchronized (instancePool) {

InstancePool.push (instance); / / if you implement the STM interface, put it on a stack

NInstances++

}}

} else {

If (! newInstance) {

CountAllocated.incrementAndGet ()

}

Return (instance)

}

}

Synchronized (instancePool) {

While (countAllocated.get () > = nInstances) {

/ / Allocate a new instance if possible, or else wait

If (nInstances < maxInstances) {

Try {

InstancePool.push (loadServlet ())

NInstances++

} catch (ServletException e) {}

} else {

Try {

InstancePool.wait ()

} catch (InterruptedException e) {

/ / Ignore

}}}

CountAllocated.incrementAndGet ()

Return instancePool.pop ()

}}

/ * *

* Load and initialize an instance of this servlet, if there is not already

* at least one initialized instance. This can be used, for example, to

* load servlets that are marked in the deployment descriptor to be loaded

* at server startup time.

, /

Public synchronized Servlet loadServlet () throws ServletException {

/ / Nothing to do if we already have an instance or an instance pool

If (! singleThreadModel & & (instance! = null))

Return instance; / / Note here, if an instance exists, it will be returned directly.

Servlet servlet

Try {

InstanceManager instanceManager = (StandardContext) getParent (). GetInstanceManager ()

Try {

Servlet = (Servlet) instanceManager.newInstance (servletClass)

} catch (ClassCastException e) {

}

If (servlet instanceof SingleThreadModel) {

If (instancePool = = null) {

InstancePool = new Stack ()

} / / here, use Stack to store the Servlet of STM

SingleThreadModel = true

}

InitServlet (servlet)

} finally {

}

Return servlet

}

How many instances will be initialized for a Servlet that implements the SingleThreadModel interface?

There are two properties in the StandardWrapper class, where maxInstance starts with 20. So there is an answer to the above question.

/ * *

* Does this servlet implement the SingleThreadModel interface?

, /

Protected volatile boolean singleThreadModel = false;/**

* Maximum number of STM instances.

, /

Protected int maxInstances = 20; at this point, the study of "whether Servlet is single or multiple" is over. I hope it can solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report