• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Servlet initialization

 
shailendra vasale
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)
 
shailendra vasale
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Satish,
Sorry, i couldn't reply immediately.
Thankx for ur reply.The way you explained help me a lot to understand.
shailendra
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic