• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

init()

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Normally only 1 instance of a servlet is created, but if we configure the web server to have multiple instance say 10 instances of a servlet A, then in that situation, how many times will the init() method be called?
1 or 10 times?
Thanks in advance for your answer.
Yogen
 
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
> "but if we configure the web server to have multiple instance say 10 instances of a servlet A"

How exactly would you configure that?
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Yogen,

init() will be called once on 10 different instances each, right?

- Gouri
 
Esam Ahmed
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I believe Gouri is right.

The most important aspect is that the instance is related to a specific servlet configuration.

If 7 request comes for a specific servlet...only one instance is created for a specific servlet.
 
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The init() method will be called once per servlet instance. so if you have 10 instances of a servlet, the init() method will be called 10 times.

> "but if we configure the web server to have multiple instance say 10 instances of a servlet A"

How exactly would you configure that?



You can do that by declaring mutiple <servlet> elements in the DD for the same servlet class.
 
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys,

Remember one thing clearly..by any chance not more than one instance of any servlet would be created by the container..also u have no means to instruct the container to do so.Only one instance of any servlet will be created by the container(no servlet will be given excuse for this!!) and when multiple request comes seperate thread will be spawned and each request from the client will be executed by the thread's service method.Init & destroy method would be executed for only on that servlet instance.

That is

* Init method will be executed when container loads & instantiates the servlet.

* Destroy method will be executed when server shuts down or when the container removes the servlet instance.

Hope it is clear!!.Plz correct me guys if am wrong..

Regards,
Priya.
 
Satish Chilukuri
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

by any chance not more than one instance of any servlet would be created by the container..



A more precise statement would be:
The container will not create more than one instance for a given servlet declaration in the deployment descriptor.

So if you have something like this in the DD:

the container will create two instances of LoginServlet.
 
Gouri Bargi
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Priya Jothi:

Remember one thing clearly..by any chance not more than one instance of any servlet would be created by the container..also u have no means to instruct the container to do so.Only one instance of any servlet will be created by the container(no servlet will be given excuse for this!!) and when multiple request comes seperate thread will be spawned and each request from the client will be executed by the thread's service method.Init & destroy method would be executed for only on that servlet instance.



This does not hold true about servlets implementing SingleThreadModel.

Though not part of the servlet specification, many web servers provide the facility to configure the number of servlet instances in case of single threaded servlet.

- Gouri
 
Esam Ahmed
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Priya,
Thanks for emphasizing on this...made me do some clarification.....
Your explanation is accurate for a single servlet assuming it is configured as:

<servlet>
<servlet-name>myServlet1</servlet-name>
<servlet-class>com.foo.theServlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>myServlet1</servlet-name>
<url-pattern>/ServletRequest.do</url-pattern>
</servlet-mapping>

What yogen really asked it not so clear to me...

Because you can also configure....like...

<servlet>
<servlet-name>myServlet1</servlet-name>
<servlet-class>com.foo.theServlet</servlet-class>
</servlet>

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

Here many <url-patter> or reuqest(s) will be for the same servlet, servlet1
(One instance of com.foo.theServlet)

Now what happens if it is like:
<servlet>
<servlet-name>myServlet1</servlet-name>
<servlet-class>com.foo.theServlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>myServlet1</servlet-name>
<url-pattern>/ServletRequest1.do</url-pattern>
</servlet-mapping>

<servlet>
<servlet-name>myServlet2</servlet-name>
<servlet-class>com.foo.theServlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>myServle2</servlet-name>
<url-pattern>/ServletRequest2.do</url-pattern>
</servlet-mapping>

Here you have the same servlet class for both servlet1 and servlet2.
You cannot say you will have two instances of the servlet class:

com.foo.theServlet

Even though you configure it with two servlet names for the same servlet class.

Does make sense to me.....
Any idea guys........


Esam
 
Esam Ahmed
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well,
Gouri is definately right
Considering my earlier example does not implement the single threaded model...inherently evil as the authors mention in HF book...

What is the scenario ?

How can yogen configure it such that it will create 10 instances while not implementing the single threaded model?

Esam
 
Esam Ahmed
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
While I was typing Satish came up with the example....
Now, according to Satish's explanation

It is possible then to have multiple instances of the same servlet class?

It was indeed a little interesting question....

Considering Satish's example and explanation....

What is the instance here?

Esam
 
Priya Jothi
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Upto my knowledge i'm sure the above configuration in DD will not create two instances for LoginServlet.Login1 & Login2 are just the names given to LoginServlet & it is used as reference only in DD not else where.They are just aliases for LoginServlet.I dont understand how the above config would create two instances.If it will create,it would be nice if someone explains how!.

Regards,
Priya.
 
Gouri Bargi
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Priya,

I checked this in Tomcat. Configuring one servlet twice as Satish has mentioned works, and two instances of the servlet get created.

Basically, I think, we are asking the container to create two servlets here, and both servlets instances happen to belong to the same class.

- Gouri
 
Esam Ahmed
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for checking this out Gouri.


Esam.
 
Ranch Hand
Posts: 378
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all,

Satish gave :

So if you have something like this in the DD:
---------------------------------------------------
<servlet>
<servlet-name>Login1</servlet-name>
<servlet-class>com.security.LoginServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>Login2</servlet-name>
<servlet-class>com.security.LoginServlet</servlet-class>
</servlet>
----------------------------------------------------

the container will create two instances of LoginServlet.

=====================================================================
=====================================================================
Esam gave :

<servlet>
<servlet-name>myServlet1</servlet-name>
<servlet-class>com.foo.theServlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>myServlet1</servlet-name>
<url-pattern>/ServletRequest1.do</url-pattern>
</servlet-mapping>

<servlet>
<servlet-name>myServlet2</servlet-name>
<servlet-class>com.foo.theServlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>myServle2</servlet-name>
<url-pattern>/ServletRequest2.do</url-pattern>
</servlet-mapping>

Here you have the same servlet class for both servlet1 and servlet2.
You cannot say you will have two instances of the servlet class:

----------------------------------------------------------

so what is the conclusion based on Esam and Satish's posts?
---------------------------------------------------------

im still new to this, but its a interesting topic where i am still exploring HFSJ. here is the quote from HFSJ page 101:


You might hear people say thing slike, "Each instance of the servlet..." but that's just WRONG. There aren't multiple instances of any servlet class, EXCEPT in one special case called SingleThreadModel, which is inherently evil...



so , my personal view is :

1. Esam's examples very good. as Esam said: the servlet class will only have one instance for servlet1 and servlet2 or even servlet3...because they are just using the same servlet class.

2. I dont think Satish's explanation would be exactly correct. Because according to quote above, There WONT be have more than one instance of the same servlet class. (not implementing SingleThreadModel. i dont know what if Satish example is refering to implementation of SingleThreadModel.)

3. if implementing SingleThreadModel, then there might have more than one instance of servlet class. correct or incorrect?
[ September 14, 2005: Message edited by: Nicky Eng ]
 
Nicky Eng
Ranch Hand
Posts: 378
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Esam Ahmed:
I believe Gouri is right.

The most important aspect is that the instance is related to a specific servlet configuration.

If 7 request comes for a specific servlet...only one instance is created for a specific servlet.



if 10 request comes for a specific servlet class...only one intance of that servlet class is created and container runs 10 threads to process multiple requests to a single servlet class. (i am referring to HFSJ, page 101 )
 
Nicky Eng
Ranch Hand
Posts: 378
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Gouri Bargi:
Hi Priya,

I checked this in Tomcat. Configuring one servlet twice as Satish has mentioned works, and two instances of the servlet get created.

Basically, I think, we are asking the container to create two servlets here, and both servlets instances happen to belong to the same class.

- Gouri



as you said that "...and two instances of the servlet get created."

i'm not understand of "two instances of the servlet get created."....were you saying 2 instance of the servlet class get created???
--------------------------
instance of servlet
instance of servlet class
---------------------------
these 2 statements are totally different right?
 
Gouri Bargi
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Nicky,

Originally posted by Nicky Eng:


as you said that "...and two instances of the servlet get created."

i'm not understand of "two instances of the servlet get created."....were you saying 2 instance of the servlet class get created???
--------------------------
instance of servlet
instance of servlet class
---------------------------
these 2 statements are totally different right?



I mean 2 instances of the servlet class.

- Gouri
 
Priya Jothi
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


I checked this in Tomcat. Configuring one servlet twice as Satish has mentioned works, and two instances of the servlet get created.

Basically, I think, we are asking the container to create two servlets here, and both servlets instances happen to belong to the same class.



Hi Gouri,

If it works thatz gr8!!.But I would request you to tell me how did u verify this(like the container created 2 instances)?.So that I'll also check in my tomcat..I also assume that ur servlet doesn't implement single thread model.

Regards,
Priya.
 
Gouri Bargi
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Priya,

I just put an SOP in the servlet constructor and init method.

This servlet is not implementing the "inherently evil SingleThreadModel".

- Gouri
 
Nicky Eng
Ranch Hand
Posts: 378
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Gouri,

Originally posted by Gouri Bargi:
Hi Nicky,

I mean 2 instances of the servlet class.

- Gouri



so you're refering to implementation of SingleThreadModel???

because according to HFSJ author:
-----------------------------------------------------
You might hear people say thing slike, "Each instance of the servlet..." but that's just WRONG. There aren't multiple instances of any servlet class, EXCEPT in one special case called SingleThreadModel, which is inherently evil...
----------------------------------------------------

there aren't "multiple instance" of any servlet class for NOT-SingleThreadModel.

please give a hint.
please give a hint.
 
Priya Jothi
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Gouri,

I really wonder how two instances have been created by ur Tomcat(Hopefully it is too generous!!! ).When I put the SOP inside init method & constructor then also i can see in my console that only one instance is getting created.Im using Tomcat5.0.28 & console output is pasted for ur reference!!




Sep 14, 2005 2:36:43 PM org.apache.catalina.core.StandardHostDeployer install
INFO: Installing web application at context path /Beer-v1 from URL file:E:\Tomcat 5.0\webapps\Beer-v1
Sep 14, 2005 2:36:43 PM org.apache.catalina.core.StandardHostDeployer install
INFO: Installing web application at context path /jsp-examples from URL file:E:\Tomcat 5.0\webapps\jsp-examples
Sep 14, 2005 2:36:43 PM org.apache.catalina.core.StandardHostDeployer install
INFO: Installing web application at context path /phone from URL file:E:\Tomcat 5.0\webapps\phone
Sep 14, 2005 2:36:43 PM org.apache.catalina.core.StandardHostDeployer install
INFO: Installing web application at context path from URL file:E:\Tomcat 5.0\webapps\ROOT
Sep 14, 2005 2:36:43 PM org.apache.catalina.core.StandardHostDeployer install
INFO: Installing web application at context path /servlets-examples from URL file:E:\Tomcat 5.0\webapps\servlets-examples
Sep 14, 2005 2:36:44 PM org.apache.catalina.core.StandardHostDeployer install
INFO: Installing web application at context path /Test11 from URL file:E:\Tomcat 5.0\webapps\Test11
Sep 14, 2005 2:36:44 PM org.apache.catalina.loader.WebappClassLoader validateJarFile
INFO: validateJarFile(E:\Tomcat 5.0\webapps\Test11\WEB-INF\lib\servlet.jar) - jar not loaded. See Servlet Spec 2.3, section 9.7.2. Offending class: javax/servlet/Servlet.class
Sep 14, 2005 2:36:44 PM org.apache.catalina.core.StandardHostDeployer install
INFO: Installing web application at context path /Tester from URL file:E:\Tomcat 5.0\webapps\Tester
Sep 14, 2005 2:36:44 PM org.apache.catalina.core.StandardHostDeployer install
INFO: Installing web application at context path /tomcat-docs from URL file:E:\Tomcat 5.0\webapps\tomcat-docs
Sep 14, 2005 2:36:44 PM org.apache.catalina.core.StandardHostDeployer install
INFO: Installing web application at context path /webdav from URL file:E:\Tomcat 5.0\webapps\webdav
Sep 14, 2005 2:36:44 PM org.apache.coyote.http11.Http11Protocol start
INFO: Starting Coyote HTTP/1.1 on http-8080
Sep 14, 2005 2:36:45 PM org.apache.jk.common.ChannelSocket init
INFO: JK2: ajp13 listening on /0.0.0.0:8009
Sep 14, 2005 2:36:45 PM org.apache.jk.server.JkMain start
INFO: Jk running ID=0 time=0/63 config=E:\Tomcat 5.0\conf\jk2.properties
Sep 14, 2005 2:36:45 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 10766 ms
Inside BeerSelect constructor
Inside init method



Any idea guys..why it is contradictory?.

Regards,
Priya.
 
Nicky Eng
Ranch Hand
Posts: 378
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Priya, i dont understand why Gouri said that will have more than 1 instance of servlet class get created......??!?!?!?!??!

because according to "HFSJ" page 101,There aren't multiple instance of any servlet class.......

what is the correct one???
 
Gouri Bargi
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Priya,

My web.xml file looks like this:



The servlet does pretty little other than printing the debug line in servlet constructor and init method. The web application name is 'Beer-v1'.

When I request for the first time to the url

http://localhost:8080/Beer-v1/SelectBeer1.do

the first instance gets created.

On first request to

http://localhost:8080/Beer-v1/SelectBeer2.do

the other servlet class instnace is created.

- Gouri
 
Satish Chilukuri
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There seems to be a lot of confusion regarding the number of instances that can be there for a servlet.

Here's what the Servlet Spec (2.4) 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."

Note the phrase - "... servlet container must use only one instance per servlet declaration".

So if you have mulitple servlet declarations for the same servlet class, the container will use multiple servlet instances.
 
Esam Ahmed
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Nicky,

I believe the confusion started with configuring the <servlet-class> with multiple <servlet-name> elements in the DD.

It is definately true that only a single instance will be created for multiple requests of a servlet (no single threded model)if the <servlet-class> is configured with a single <servlet-name> element.

Then the confusion again, how does the container behave if the same <servlet-class> is configured with multiple <servlet-name> elements.

It is plausible to think that perhaps multiple instances (never say that!!) will be created if the servlet class is referenced thrugh multliple servlet names ...

If the authors are right even after considering the above combinations, then the container must keep track of the servlet names that map to the same servlet class...

Then only one instance!!!

Esam
 
Esam Ahmed
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As per spec....then...

Multiple instances of a servlet class is possible with multiple servlet declarations in the DD.

But perhaps we mixed up the instances of a single declarion with multiple declarations for a same servlet class???

Esam.
 
Gouri Bargi
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Satish for giving exact wordings from the servlet specs. I hope we do not have any more confusion here.

- Gouri
 
Priya Jothi
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

I got it what Gouri said!!!.Sorry I missed out the declaration part in web.xml.As Sathish said for each servlet declaration & its corresponding mapping(mapping is must & it should be different!!) one servlet instance is created by the container.

My web.xml :

<servlet>
<servlet-name>Ch3 Beer</servlet-name>
<servlet-class>BeerSelect</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>Ch3 Beer</servlet-name>
<url-pattern>/SelectBeer.do</url-pattern>
</servlet-mapping>

<servlet>
<servlet-name>Ch31 Beer</servlet-name>
<servlet-class>BeerSelect</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>Ch31 Beer</servlet-name>
<url-pattern>/SelectBeer1.do</url-pattern>
</servlet-mapping>


Note the items above bolded.They are different.When u access the servlet by using "/SelectBeer.do" one instance of "BeerSelect" will be created by the container.When u access the same servlet using "/SelectBeer1.do" another instance will be created.Console output is conforming the same!!

Hope this clears all our doubts!!

Thanks a lot to Yogen for starting such an interesting thread & and to all active participants(just to name a few - Gouri,Esam,Sathish...).

Am very happy that i've learnt one new thing today!!!

Regards,
Priya.
 
Ranch Hand
Posts: 1026
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Everyone

Follow this link

https://coderanch.com/t/361131/Servlets/java/Servlet-init

Bear(Hey thats his NAME) has given a precise explanation for this one.

 
Nicky Eng
Ranch Hand
Posts: 378
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all,

in the end, i also understood with the explanation and example given by Priya....

also thanks to other members here....thanks guy....finally clear !
 
reply
    Bookmark Topic Watch Topic
  • New Topic