It's not a secret anymore!*
The moose likes Servlets and the fly likes doubt from Head First Servlets & Jsp's book Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "doubt from Head First Servlets & Jsp Watch "doubt from Head First Servlets & Jsp New topic
Author

doubt from Head First Servlets & Jsp's book

Ravuru Naresh
Greenhorn

Joined: Apr 17, 2008
Posts: 1
Hi.I was going through Head First Servlets and Jsp's. From page 97,it is said like the init() method wud be called on the instance of the sevlet. And the service() method would be called when the first request comes in. Ther's nothing to do inside the server unless and until the server gets a user request. So what do you think about servlet's instantiation?? does the container instantiates all the servlets as soon it (web container / web server) is started. if not i request you to clarify my doubt.And also what do you mean by initialized?? . When the init() mehtod will be called? the service() method is called when the first request comes in!! So when would be the init() method called? Thanks in advance.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60782
    
  65

Originally posted by Ravuru Naresh:
the init() method wud be called

Please use real words when posting to the forums. Abbreviations such as "wud" in place of "would" only serve to make your posts more difficult to read and less likely to generate useful responses.

Please read this for more information.

thanks,
bear
JavaRanch sheriff


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60782
    
  65

The Servlet Spec dictates that the init() method be called before the first request. Exactly when is up to the container. You can also give hints to the container in the web.xml.
Francesco Bianchi
Ranch Hand

Joined: Jun 22, 2007
Posts: 62
A servlet is first of all a class. As every class it must be instantiated before being used (that is the constructor is called).

Once instantiated, a Servlet still needs to do some operations to be able to act as a Servlet. At minimum it has to have access to its own ServletContext instance. That is injected by the container during initialization phase, usually right after it has been instatiated. That is also the point where you should put all your "run-once" code.

So: you define a class which happens to be a Servlet. To use it you first have to get an instance (constructor()), then to teach it what it needs in order to act as a servlet (init()) and only at that point you can make it properly work (by calling service() method)

If you look through Servlet interface you will find 2 init() methods...

But I think now you can go back to the optimus HFSJ, Chap. 4


SCJP 5 & 6, SCWCD 5, SCBCD 5
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: doubt from Head First Servlets & Jsp's book
 
Similar Threads
destroy() method call in sevlet's life cycle
Chapter 2 (HFSJ) notes , may be useful for anyone
what happens when the first request comes to the webserver?
Servlet Container
question regarding the sequence of events happening as given in head first jsp servlet