This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Servlets and the fly likes Servlet initialization Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlet initialization" Watch "Servlet initialization" New topic
Author

Servlet initialization

shailendra vasale
Ranch Hand

Joined: Dec 17, 2000
Posts: 38
hi All,
When any Servlet gets loaded the init(--) method called first though we may not override it or make a call using "super" keyword.
The jsdk2.0 API states that - "The init method is called once, automatically, by the network service each time it loads the servlet."
Can anyone plz explain,how the init(--) mehtod gets called?
Thankx in advance.
shailendra
prakash muthu
Ranch Hand

Joined: Sep 06, 2000
Posts: 75
Hi,
If your code invoke a particular servlet, it will check that whether the servlet is loaded already or not by using naming services. If its available then it won't invoke the init(). other wise it will invoke the init().
let me know if i'm wrong or if you need more info
-Prakash
(prakashem@rediffmail.com)


Prakash.M<BR>Bangalore- India<BR>(prakashem@rediffmail.com)
shailendra vasale
Ranch Hand

Joined: Dec 17, 2000
Posts: 38
hi Prakash,
I know that the servlet gets intialized when first request is made and if the servlet is running then for the subsequent requests,it won't get initialized again.
What i want to know is - for the first request itself how the init(--) method gets called if we are neither overriding the init(--)method nor calling it using "super" keyword.
e.g.

public class MyServlet extends HttpServlet
{
public void doGet(HttpServletRequest req,HttpServletResponse res)throws ServletException,IOException
{
//code
}
}
Now in above example,how the init(--)method gets called when the first request is made.

Hope,this would have cleared what i want to ask.
shailendra.
[This message has been edited by shailendra vasale (edited December 20, 2000).]
Satish Kasala
Greenhorn

Joined: Dec 19, 2000
Posts: 16
Hi shailendra,
if your servlet does not override the init method of its super class in this context HttpServlet, in that case superclass
init() method is called.
Satish
shailendra vasale
Ranch Hand

Joined: Dec 17, 2000
Posts: 38
hi Satish,
Thankx for ur reply.But as we knows,the method of super class doesn't get called automatically.It can be called using the "super.init(--)" keyword and in the program "MyServlet.java" I had not used the "super" keyword,then also the init(--) method gets called,how?
As per API,as mentioned earlier,I think it's related to some network sevice,but I am unable to understand it.
Can anyone plz explain it.Thankx in advance.
shailendra
prakash muthu
Ranch Hand

Joined: Sep 06, 2000
Posts: 75
Hi Sailendra,
How you will know the control is invoking init() with out overwriting the init also. If you can write a servlet with out overwriting init() also. If you need anything relevent to servlet configuration, then only you need the init method. Other wise servlet will run, with out over writing the init.
I think it will helpful to you.
Let me know if you need more info
-Prakash
(prakashem@rediffmail.com)
shailendra vasale
Ranch Hand

Joined: Dec 17, 2000
Posts: 38
hi Prakash,
Thankx for ur reply.I know my query is a bit tricky[may be related with some network service or servlet engine].I know the functionality of init(--) mehtod.But,what I want to know is how the init(--) gets called implicitly on first request.
Thankx.
shailendra
Satish Kasala
Greenhorn

Joined: Dec 19, 2000
Posts: 16
shailendra,
you are getting close to the answer for your own question. The call to init method is something related to the servlet engine. The servlet engine first call to a servlet when it is invoked would be through the init(ServletConfig config) and the servlet engine would pass in the ServletConfig object to the method.
Most of the times you call super class ie HttpServlet init() method through our init() method so that after we are done initializing, rest of the initialization would be done by the super class init() method. If the servlet engine is not able to find an init in your servlet then it tries to look up the super class init() method and try to run it.
Hope it answers your tricky question
Satish
shailendra vasale
Ranch Hand

Joined: Dec 17, 2000
Posts: 38
hi Satish,
Sorry, i couldn't reply immediately.
Thankx for ur reply.The way you explained help me a lot to understand.
shailendra
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Servlet initialization
 
Similar Threads
Final value in a Servlet
Why dont we use constr instead of init method ?
Init method getting called multiple times
Servlet Context Listener
If two clients call the same servlet...