aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Servlet Life Cycle doubt?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Servlet Life Cycle doubt??" Watch "Servlet Life Cycle doubt??" New topic
Author

Servlet Life Cycle doubt??

Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9349
    
    2

Hi Guys,

I got this question from David Bridgewater's book,

Identify statements that are always true about threads running through the service() method of a servlet with the following class declaration

public class MyServlet extends HttpServlet {...servlet code here...}

A. The destroy method never cuts short threads running through the service() method.
B. Threads running through the service() method must run one at a time
C. There could be anything from 0 to many threads running through the service() method during the time the servlet is loaded.
D. The init() method for the servlet hasn't run, no threads have yet been able to run through the service() method.
E. Atleast one thread will run through the service() method if the init() has been executed.

Well, I guessed the answer to be D and E as I have to choose 2 options. But they say it is C and D. How can C be correct?? Can anyone justify??

Thanks in advance.


SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
Tha�s Manfrim Firmino
Ranch Hand

Joined: Apr 12, 2004
Posts: 48
When the containers starts or when the first client makes a request, the container creates one instance of the servlet, call init(), and then service() for that request. After that, for each request, the container will create a thread for that servlet and call service(). Is it clear for you? Continuing..
Imagine there is only one client that makes a request for that servlet(). Then the container creates a thread, run service() and finish for that client. There are no more running threads for that servlet until another client makes a request for that servlet.
The servlet lives until the container shuts down, and not while there are requests for it. It explains why C is ok and E is wrong.


Tha�s M. K. M. Firmino
Tha�s Manfrim Firmino
Ranch Hand

Joined: Apr 12, 2004
Posts: 48
Maybe I was not clear because I could not read again what you asked while typing the answer...

Well, there are containers that can initiate the servlets prior to the first request to optimize performance.
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9349
    
    2

Thais,

E says that if the init() method has run then it means that someone has made a request for the servlet. So the container will create a seperate thread for it and run to finish. So according to this why option E shud not be a choice for the question given??
Purna Chandra Rao
Greenhorn

Joined: Feb 06, 2006
Posts: 23
Hi,

I think the questions given is partially dependent on the type of Web container.
There are web containers which loads the servlet at the first request(Lazy Loading) and there are conatiners which loads the servlet when the server starts.(Pre Loading).

Option c seems to be correct if it is Lazy Loading.
Option e also seems to be correct if it is Lazy Loading.

Only option d seems to be fully correct
Thanks and Regards,
Purna Chandra Rao
SCJP 1.4
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9349
    
    2

Purna,

Option C says that, "There could be anything from 0 to many threads running through the service() method during the time the servlet is loaded" SO as per what you say, when it is Pre Loading, only the init() method is called. Check the option C talks about service() method. I guess that today there is no such container that would call the service() method during startup itself!!
Aniket Patil
Ranch Hand

Joined: May 02, 2006
Posts: 218
There could be anything from 0 to many threads running through the service() method during the time the servlet is loaded.

Jothi,

I understand what you are trying to hint. However, I believe this statement is being interpreted differently by Purna and you.

Assuming "during the time servlet is loaded" means "servlet is already loaded and ready to service requests", then option C appears reasonable. You can have no request for a servlet or multiple threads executing at the same time. This would be the case for both JIT and early instantiation.
[ January 17, 2007: Message edited by: Aniket Patil ]

SCJP 5.0 | SCWCD 1.4 <br /> <br />If you don't know where you are going, any road will take you there!
Purna Chandra Rao
Greenhorn

Joined: Feb 06, 2006
Posts: 23
Hi Jyothi,
Option c tell that There could be anything from 0 to many threads running through the service() method during the time the servlet is loaded.
If it is Lazy loading the instance of the servlet is created and its init method is called at the first request and this first request inturn makes the service method to call and there could be more than one requests to that servlet at that same point of time.Thats why i told option c was correct
Please correct me if i am wrong.

Thanks and Regards.
Purna Chandra Rao.
SCJP 1.4
[ January 17, 2007: Message edited by: Purna Chandra Rao ]
Krishna Pisupati
Greenhorn

Joined: Jan 21, 2007
Posts: 1
Hi,

I think option C is gud only for the containers that follow Lazy Loading. I agree with Purna on this. Moreover, it's an option that could/couldn't be provided by the container vendors. It's not a hard and fast rule as per the Servlet Specification.

For example, in WebLogic you have an option in weblogic.xml to specify load on startup. In this case, servlet would be loaded during the container startup the servlet would be loaded into the memory and subsequently the init() method would be executed. If you put this option to false, the class would be loaded when the first request comes in. Hence in case of preload option set to true, C is correct otherwise C is wrong.

In any case, this is partly container dependent question.


As per Aniket's comment also.......
It sounds container dependent.....
Assuming "during the time servlet is loaded" means "servlet is already loaded and ready to service requests", then option C appears reasonable.

In this case also, how can you think the servlet was already loaded?? Was it loaded when the first request came in or while container startup because of this perload option set to true?? If it is loaded when the first request came in, that question does not make sense ... because I can ask the same question what happens after serving 10 requests? Still Zero or more threads can execute service() method.

In any case, my understanding is, it is container dependent which is not clearly mentioned in the specification but left as an option to the container vendors. The answer to the above question seems to be D in my opinion.....

Please correct me if I am wrong.


Krishna.

[ January 21, 2007: Message edited by: Krishna Pisupati ]
[ January 21, 2007: Message edited by: Krishna Pisupati ]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Servlet Life Cycle doubt??