• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Regarding the init() methods

 
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Sony Agrawal
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ankit Garg
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.

 
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic