wood burning stoves 2.0*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes init() method in a Servlet's lifecycle. 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 "init() method in a Servlet Watch "init() method in a Servlet New topic
Author

init() method in a Servlet's lifecycle.

ragi singh
Ranch Hand

Joined: Mar 10, 2010
Posts: 198

Hi all ,

During the lifecycle of a servlet init() method is called . Now there are two init() methods one that takes a ServletConfig as argument and the other which does not take any argument . Which of the two is the default that a constructor calls when a servlet is initialized . and when is the init() method with the ServletConfig argument called ?
Mark Uppeteer
Ranch Hand

Joined: Mar 02, 2004
Posts: 159

the one with the ServletConfig is called by the container and has the properties in it from the web.xml.
You shouldn't touch that one.
The init(Servletconfig) will call the other init() where you get your shot to initialize.
Don't override init(Servletconfig) without calling super.init(ServletConfig).


I know where my towel is. (SCJP 5, OCPJWCD)
[Free Quiz Tips for a fun night with friends or family] Flash games
Mark Uppeteer
Ranch Hand

Joined: Mar 02, 2004
Posts: 159

I'm a bit confused with your...
Which of the two is the default that a constructor calls when a servlet is initialized

the init is called by the container, not by a constructor
ragi singh
Ranch Hand

Joined: Mar 10, 2010
Posts: 198

I am sorry about that Mark its container and not constructor . Does that mean when my servlet is initialized by the container , after calling the servlet constructor the container calls the init(ServletConfig) and it is in this init(ServletConfig) that the init() method is called .
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

init(ServletConfig) is the template method, which stores ServletConfig, and then delegates to init(). This is what javax.servlet.GenericServlet.init(ServletConfig) looks like:


And, if you really want to override init(ServletConfig), make sure you invoke super.init(ServletConfig);, usually as the first line, though it's not strictly required.


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Mark Uppeteer
Ranch Hand

Joined: Mar 02, 2004
Posts: 159

Does that mean when my servlet is initialized by the container , after calling the servlet constructor the container calls the init(ServletConfig) and it is in this init(ServletConfig) that the init() method is called .


yes ragi singh,
its like that (and thats the way it is )
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: init() method in a Servlet's lifecycle.