Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

servlet element in web.xml

 
Ernesto Leyva
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello

Someone knows if the following declaration
is it valid in web.xml?

 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it looks valid to me
 
Shameen MK
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe that you will get an error when the request actually tries to find the servlet. It will give this kind of an error, since it cannot find a match in the servlet class -

The requested resource (url pattern) is not available.
 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is possible to configure the same servlet under different names. One possible reason to do this might be if you want to use different init parameters.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shameen,
Are you confusing servlets definitions and servlet mappings ?
You can configure different mappings, one for FirstInstance and another one for SecondInstance. There should be no problem with the definition above.
 
Vivek Kinra
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with Satou...
 
Shameen MK
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oops, I overlooked that in a hurry. Thanks for correcting me.
 
Yogesh Hingmire
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah i think it is valid and will create two spearate instances of the servlet...
 
Ernesto Leyva
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Acutally the point is to clarify if the container
will create two instances. I read in HJSC the container
will create only one instance of a servlet
 
Narendra Dhande
Ranch Hand
Posts: 951
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Yes, the container creates two seperate instances.

Thanks
 
Shameen MK
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Ernesto mentioned, may not be two different instances, but two differenct threads for the service() method for each request.
 
Madhu Sudhana
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Raunchers

For a servlet in the container there would be only one instance in the container,

as requests come in for the servlets conatiner creates a new thread and allocates to the coming request

This is what I got from HFSJ.

correct me if I am wrong
 
Vikrant Pandit
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ranchers,

I think there will be two instances in this case.

That the only way container would be able to pass on two different sets of init parameters
 
Narendra Dhande
Ranch Hand
Posts: 951
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

This Thread may helpful to you.

When you define two <servlet> entries containing the same class as a <servlet-clss>, two seperate instances will be created. There ServletConfig objects are different in init method. As mentioned above answers, it will help you to set up different initial parameteres for each servlet instance.

Thanks
 
Ernesto Leyva
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I ran a test in Tomcat5.5

I created the following servlet:



then I put the web.xml as stated in this thread (two <servlet> elements) for the same servlet) and guess what I got two different screens so yes Tomcat did create two instances. Now I am trying to search in the specification to see if this is mentioned somewhere.
 
srilatha kareddy
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i too tested it.
instances created = servlets declared in Deployment descriptors
 
Akshay Kiran
Ranch Hand
Posts: 220
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic