• 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

choosing between init() and a constructor

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
can we do all the initilization in the constructor of a servlet instead of init()??...if not why?
Thanx in advance...
 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Servlets do not have constructors - well, not constructors that you can use. Why? Because they are container managed objects, its up to Tomcat (or whatever container you choose) to construct instances of the servlet. You use init() because it represents actions performed during initialization of the servlet, which is different from construction.
 
Bis Bang
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Jhon...I think u r mistaking...A servlet can have a constructor(default).. U try to write a servlet with a constructor and init() it will compile and run..No doubt about that...
[ March 11, 2004: Message edited by: Bis Bang ]
 
Ranch Hand
Posts: 157
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi bis
No doubt u can create constructor in servlet ,servlet will compile and run. but you have to call your constructor explicitly as servlet engine will go only for methods described for life cycle of servlet. So in that case what ever you write in your servlet outside life cycle , it would be treated as a normal method by Servlet Engine which could be invoked explicitly.
Most important point is Servlet instance is to be created by Servlet Engine , constructor never comes into picture in that creation process.
Think of init method in Servlet a replacement for constructor in normal java class.

Originally posted by Bis Bang:
Hi,
Jhon...I think u r mistaking...A servlet can have a constructor(default).. U try to write a servlet with a constructor and init() it will compile and run..No doubt about that...
[ March 11, 2004: Message edited by: Bis Bang ]

 
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Remember the following :
When the servlet container starts up, the following can happen :
1. the servlet container looks into the deployment desciptor for a servlet definition.
2. the servlet container instantiates the servlet by calling Class.forName(servlet-class).createInstance(), the no-arg constructor is called.
3. the servlet container calls the init(ServletConfig)-method on the servlet instance. The init(ServletConfig) method stores a reference to ServletConfig and calls the init()-method on the servlet intance. The ServletConfig object contains all the initialization parameters specified for this servlet in the deployment descriptor.
4. the servlet container calls the service(ServletRequest, ServletResponse)-method of the servlet as needed for processing client requests.
5. .....

Therefore a no-arg constructor can be specified in the servlet-class,
and the no-arg constructor will be called. So some initialization can be done. Also remember that a servlet can have some specific init-parameters that can be specified in the web.xml for this specific servlet.
These parameters are wrapped into the ServletConfig object, which is available when the init()-method is called.
Therefore initialization of a servlet by using it's initialization parameters should be done in the init()-method.
When I'm not specific enough or you have more questions, just ask.
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Bis,
All servlets have a default no-args constructor, which gets called before the init(...) method. The Servlet container can't know what sort of parameters you're expecting as part of the servlet-instance construction step, so it has to assume you have a no-arg constructor that's available to create a "default" instance of the servlet.
There are 2 reasons you use the init() method as opposed to using the constructor:
1) The Servlet container can't know what sort of parameters you`re expecting (as part of the servlet-instance construction step), so it has
to assume you have a no-arg constructor to create a "default" instance of the servlet.
2) Because of the above reason, if parameters are needed, a different mechanism needs to be used (getServletContext()) and a slot to do it in (during the init() method after the servlet has been constructed).
I guess its a way of keeping the API as simple as possible, for everyone involved. You could use the default constructor but not parameters from the ServletContext in the constructor (not portably, anyway). Because the constructor of a Servlet (or Filter) doesn't have access to the ServletConfig (or FilterConfig) object, so it cannot acquire initialization parameters, a reference to the ServletContext instance, or anything like that.
Hope that helps.
 
Ranch Hand
Posts: 5399
1
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Bis Bang:
Hi,
Jhon...I think u r mistaking...A servlet can have a constructor(default).. U try to write a servlet with a constructor and init() it will compile and run..No doubt about that...


Can you have construtor with argument and use it for initializing servlet ??
Here comes init() and servletConfig object in to the picture.
As servlets are loaded dynamically by servlet engine, init and ServletConfig are the means to pass information to the servlet as you cant use constructor with arguments for dynamic loading of class.
HTH.
 
Bis Bang
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Aneesha and RK.. yeah we can not have constructor with parameter in servlet...If we create a servletconfig object in the difault consructor; can we have the features of init() method in that case!!! or some how there will be any life cycle violation??...
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Arnold


Remember the following :
When the servlet container starts up, the following can happen :
1. the servlet container looks into the deployment desciptor for a servlet definition.
2. the servlet container instantiates the servlet by calling Class.forName(servlet-class).createInstance(), the no-arg constructor is called.
3. the servlet container calls the init(ServletConfig)-method on the servlet instance. The init(ServletConfig) method stores a reference to ServletConfig and calls the init()-method on the servlet intance. The ServletConfig object contains all the initialization parameters specified for this servlet in the deployment descriptor.
4. the servlet container calls the service(ServletRequest, ServletResponse)-method of the servlet as needed for processing client requests.
5. .....


I want to know the rest of the steps that the container does, like calling service(..., ...) method which calls doGet() or doPost() etc..
In general, I'd like to know "how (or in what order) servlet container calls the servlets methods (life-cycle methods, constructor, etc)". Any link on this also would be greatly appreciated.
 
Aneesha Singh
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Bis,
I dont think that can work. You see, the ServletConfig object is created by the Servlet Container and only passed to the init method as an argument. The ServletConfig object contains Servlet parameters and a reference to the Servlet's ServletContext. It wont be logical to create a new object for this in the constructor.
And why would you want to do that anyway?
Cheers!
 
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The answer lies in the Servlet Spec (I refer to Servlet 2.3, i.e. J2EE 1.2, pp. 23-24).
"SRV.2.3.1 Loading and Instantiation
The servlet container is responsible for [...] instantiating servlets. [...]
After loading the Servlet class, the container instantiates it for use.
SRV.2.3.2 Initialization
After the servlet object is instantiated, the container must initialize the servlet before
it can handle requests from clients. [...] The container initializes
the servlet instance by calling the init method of the Servlet interface with a
unique (per servlet declaration) object implementing the ServletConfig interface."

In a nutshell:
- Instantation means that a constructor has to be invoked; this is the nature of Java. The spec does not mandate the existence of a public no-arg constructor. This means that if for some (pedantic) reason you contrive a servlet class with either a single private no-arg constructor or a single constructor with args (or a fortiori a single private no-arg constructor), the servlet container is supposed to able to handle it. Which constructor the container may use in the presence of many constructors is up to the container's discretion, but it seems that the no-arg constructor (if any) would be the logical choice.
- Initialization (whatever is within the init() method) will occur after instantation, i.e. after the Servlet object has been constructed. You can't avoid instantation, and, if you have a single public no-arg constructor, it is definite that whatever code in the container will get executed.
Panagiotis.
[ March 15, 2004: Message edited by: Panagiotis Varlagas ]
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi BIs, I need to ask because you have such a unique name, but how did you get that name?
Thanks
Mark
 
Bis Bang
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanx Aneesha and all.... for giving me all the explanation about the topic..
Hey Mark Do U like the name... Its stands "Biswajit(Bis) Bangalore(Bang)" Creativity.....!!!
 
Mark Spritzler
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Very clever.
I was afraid it wasn't your real name and have to jump on you for not following our naming conventions.
Mark
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Didn't realize this discussion was taking place. I posted this question under another topic...Anyhow,
What happens when you create instances of servlets within another class..
I have a class that when instantiated has a member variable which is of type servlet--calling the default constructor. Does the container create another servlet? I'm looking at someone elses code and trying to explain to them that this isn't the way servlets were designed to work..besides pointing them to section 2.2 of the specification (2.3 Servlet API).
What actually happens when they do this?
[ March 18, 2004: Message edited by: Vasilis Karas ]
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic