siva636
siva636

Reputation: 16441

Servlet instances across multiple mappings

If one Servlet is mapped twice, using two different names (as shown bellow) how many instances of the Servlet will be created by the container?

One instance or two or it is container dependent?

<servlet>
    <servlet-name>servlet1</servlet-name>
    <servlet-class>com.me.servlet.MyServlet</servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>servlet1</servlet-name>
    <url-pattern>/page1.html</url-pattern>
</servlet-mapping>


<servlet>
    <servlet-name>servlet2</servlet-name>
    <servlet-class>com.me.servlet.MyServlet</servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>servlet2</servlet-name>
    <url-pattern>/page2.html</url-pattern>
</servlet-mapping>

Upvotes: 1

Views: 1678

Answers (3)

McDowell
McDowell

Reputation: 108899

<servlet>
    <servlet-name>FooServlet</servlet-name>
    <servlet-class>foo.FooServlet</servlet-class>
    <init-param>
        <param-name>foo</param-name>
        <param-value>bar</param-value>
    </init-param>
</servlet>

Since each servlet has its own configuration, there must be at least one servlet instance per servlet definition.

The Servlet 3.0 specification says:

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

SingleThreadModel should be avoided.

Assuming a non-distributed environment, the posted descriptor fragment will result in two servlet instances.

Upvotes: 2

Ramesh PVK
Ramesh PVK

Reputation: 15446

By default servlets are not thread safe unless you implement javax.servelt.SingleThreadModel (which is deprecated).

Many containers create a single instance for a servlet. ServletMapping has nothing to do with the number of instances that would be created. ServletMapping just map to the servlet defined using <servlet> tag.

If you have multiple <servlet> tags for the same servlet class with different names. Yes, it will create multiple instances for that servlet. But now when you have mulitiple mappings.

Upvotes: 1

Kyrra
Kyrra

Reputation: 482

It will most likely depend on the implementation of the servlet container. With whatever you are intending to do, you should assume that a single instance of your servlet will be accessed by multiple threads at once, no matter your servlet-mapping configurations.

Upvotes: 0

Related Questions