Menelaos
Menelaos

Reputation: 25725

Multiple Servlet Mappings

I've opened up a web.xml file in a project I have been assigned to, and am seeing multiple servlet mappings with the same servlet name:

   <servlet-mapping>
        <servlet-name>action</servlet-name>
        <url-pattern>/beta/*</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>action</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>

Additionally, the specific servlet seems to include two different configurations:

<servlet>
        <servlet-name>action</servlet-name>
        <servlet-class>org.common.utils.HeadCompliantActionServlet</servlet-class>
        <init-param>
            <param-name>config</param-name>
            <param-value>/WEB-INF/struts-config.xml</param-value>
        </init-param>
        <init-param>
            <param-name>config/beta</param-name>
            <param-value>/WEB-INF/struts-config-beta.xml</param-value>
        </init-param>

So my question is this...

Are two different instances of the specific servlet being initialized/loaded? Or is one versions of the specific servlet being loaded?

Upvotes: 0

Views: 7315

Answers (1)

DanielBarbarian
DanielBarbarian

Reputation: 5323

This section

<servlet-mapping>
    <servlet-name>action</servlet-name>
    <url-pattern>/beta/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>action</servlet-name>
    <url-pattern>*.do</url-pattern>
</servlet-mapping>

specifies which patterns your servlet should respond to. It is quite all right to have multiple url patterns using the same servlet as in your case here.

And then this section:

<servlet>
    <servlet-name>action</servlet-name>
    <servlet-class>org.common.utils.HeadCompliantActionServlet</servlet-class>
    <init-param>
        <param-name>config</param-name>
        <param-value>/WEB-INF/struts-config.xml</param-value>
    </init-param>
    <init-param>
        <param-name>config/beta</param-name>
        <param-value>/WEB-INF/struts-config-beta.xml</param-value>
    </init-param>

This is only definition for one servlet and not two. However it has two parameters, the ones defined with init-param sent to the servlet on init, but it is still the one same servlet definition.

Struts then reads the configurations and handles those within the servlet initialized.

How many instances of your servlet class you will have in your application is for the servlet container to decide.

Upvotes: 1

Related Questions