posted 18 years ago
Ernesto, it is a well known fact that the container creates an instance of every <servlet/> it encounters in the DD,
In the SCWCD study kit, it is clearly mentioned that this technique maybe used to create instances of servlets with different init-params
forexample,
you want to initialize servlet A with info for DataBase 1 & its username pass
say you map it to /servA/DB1,
but at another path you would also want another path where the same servlet is initialized for to be able to access DB2 with the appropriate username and pass-> /serrA/DB2
Instead of creating 2 servlets, you create one servlet but put 2 <servlet/> tags in the DD specifying the appropriate init-params
<servlet>
<servlet-name>serv_a_DB1</servlet-name>
<servlet-class>my.org.ServletA</servlet-class>
<init-param>
<param-name>username</param-name>
<param-value>spacey</param-value>
</init-param>
<init-param>
<param-name>password</param-name>
<param-value>snakeeyes</param-value>
</init-param>
</servlet>
<servlet>
<servlet-name>serv_a_DB2</servlet-name>
<servlet-class>my.org.ServletA</servlet-class>
<init-param>
<param-name>username</param-name>
<param-value>tracey</param-value>
</init-param>
<init-param>
<param-name>password</param-name>
<param-value>venus123</param-value>
</init-param>
</servlet>
and then you configure the two servlets for ur mappings
<servlet-mapping>
<servlet-name>serv_a_DB1</servlet-name>
<url-pattern>/servAoracle</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>serv_a_DB2</servlet-name>
<url-pattern>/servAmysql</url-pattern>
</servlet-mapping>
thats it, now you can make the same servlet query different databases by just changing the request url