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.
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