aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Regarding the init() methods 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 "Regarding the init() methods" Watch "Regarding the init() methods" New topic
Author

Regarding the init() methods

Sony Agrawal
Ranch Hand

Joined: Oct 04, 2009
Posts: 143
I am reading the HFSJ book ...I ended up with some confusion about the order of the methods

1. I have read that the init() method is called by the init(ServletConfig) method but in HFSJ page 97 they have only mentioned about the init() method being called by the container. When is the init(ServletConfig) is called? I mean the order? OR did they actually mean the init(sevletConfig) in the book.

2. We are not supposed to write any constructor in a servlet. Why is that?. It say we can override the init() INSTEAD of writing the constructors.What is that we are trying to avoid by not writing the constructor.

3. And also i read that when the init(ServletConfig) is overridden , the first statement should be super.init(). Why is it required to be the first statement in the overridden method?

4. However the init()(no argument) method does not have any statments in tomcat implementation. So what does running the init() mean?


Thanks
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9280
    
  17

Sony, the init(ServletConfig) method is called by the container not init(). The init() method is convenience method for us to override. If we override the init(ServletConfig) method, then we'll have to store the ServletConfig instance as an instance field and return it in getServletConfig method. Basically if we override the init(ServletConfig) method, we'll have to do this
The init(ServletConfig) implementation in GenericServlet class saves us from this trouble by doing this automatically. This is why if you decide to override the init(ServletConfig) method, you should generally call super.init(config); so that we don't have to worry about saving the ServletConfig instance. The init(ServletConfig) method of GenericServlet class calls the init() method. So if we override the init() method, we can execute our own initialization code, and don't have to worry about storing the ServletConfig or calling super.init(config);. So most of the times you'll override the init() method. You can create a constructor for your Servlet class, but at that time, the ServletConfig instance is not ready and other resources might not be available. So generally its better to do your initialization logic in the init method when everything is ready...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Srikanth Nalam
Greenhorn

Joined: Feb 23, 2010
Posts: 20
Hi Sony,

1.Yes. They actually mean that container calls init(sevletConfig) method.

2.Servlet loading and initilization happens once. For any further requests a thread will be created and service method will be called.
By not allowing to write constructor may be we are tyring to convey that each call to servlet won't create a seperate servlet object.

3&4. The init() method is basically for servlet developers to override and not to bother about ServletConfig.


Thanks & Regards,
Srikanth Nalam.
Sony Agrawal
Ranch Hand

Joined: Oct 04, 2009
Posts: 143
Thanks ankit, i understood now
but couple of questions more
Ankit Garg wrote: You can create a constructor for your Servlet class, but at that time, the ServletConfig instance is not ready and other resources might not be available.

1. Can you give me an example of the other resources that you are talking about (I am not able to figure out those resource )



2. when are the ServletConfig and ServletContext objects created?
The answers that popped is (please confirm)
ServletConfig()--- When a request for the servlet is received for the FIRST time? followed by constructors of the servlet being run.
ServletContext()-- When application is deployed.

3. If i override the jspInit(), then this method would be called from init(SevletConfig).Please confirm . Let me know if i am wrong
James Tharakan
Ranch Hand

Joined: Aug 29, 2008
Posts: 580

Sony Agrawal wrote:
Ankit Garg wrote: You can create a constructor for your Servlet class, but at that time, the ServletConfig instance is not ready and other resources might not be available.

1. Can you give me an example of the other resources that you are talking about (I am not able to figure out those resource )



2. when are the ServletConfig and ServletContext objects created?
The answers that popped is (please confirm)
ServletConfig()--- When a request for the servlet is received for the FIRST time? followed by constructors of the servlet being run.
ServletContext()-- When application is deployed.

3. If i override the jspInit(), then this method would be called from init(SevletConfig).Please confirm . Let me know if i am wrong



Eager to know answers for sony's question


SCJP 6
Why to worry about things in which we dont have control, Why to worry about things in which we have control ! !
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9280
    
  17

1. The other resources can be anything. Like if you have dependency injections in your servlet (eg. using @EJB annotation) etc, then the container will be able to inject those resources after it creates an instance of the servlet. So they will be null when the constructor is called. So you are better of using init method.

2. ServletConfig instance is created before the first request for the servlet is serviced. If the servlet has load-on-startup value set, then the servlet will be loaded (and the ServletConfig instance created) when the application is deployed. The ServletContext instance is created when the application is deployed.

3. Yes the jspInit method is called by the init method. The JSP pages that we use in Tomcat extend the org.apache.jasper.runtime.HttpJspBase class. If you checkout the source code of that class, you'll see that there is a call to jspInit()...
James Tharakan
Ranch Hand

Joined: Aug 29, 2008
Posts: 580

Thanks Ankit.
Ankit Garg wrote:If the servlet has load-on-startup value set, then the servlet will be loaded (and the ServletConfig instance created) when the application is deployed.


I am not sure of the load-on-startup thing...... but if that value is NOT set then when will the servletConfig object created?
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9280
    
  17

James Tharakan wrote:I am not sure of the load-on-startup thing...... but if that value is NOT set then when will the servletConfig object created?

If the value is not set, then generally the container creates the ServletConfig instance when the servlet is requested. But the container is free to initialize the servlet before that (as far as I could interpret the specifications). This is what the spec says
The servlet container is responsible for loading and instantiating servlets. The loading and instantiation can occur when the container is started, or delayed until the container determines the servlet is needed to service a request.
Brij Garg
Ranch Hand

Joined: Apr 29, 2008
Posts: 234
load-on-startup is an element in the deployment descriptor. Every servlet element has load-on-startup element.


when load-on-startup emelent is set to 1 then that servlet will be loaded whenever server starts.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Regarding the init() methods
 
Similar Threads
Questions about ServletContext and ServletConfig
Mechanism of init method to initialize servlet
init() and http related questions
Overriding both versions of init() method
init() method in a Servlet's lifecycle.