• 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

Doubt- Init() method

 
Ranch Hand
Posts: 111
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

What is the real need of "init" method in servlet, the constructor of the servlet can serve the same purpose, what EXTRA thing this init method provides which cannot be implemented with constructor?


Regards,

Abdul Mohsin
[ June 06, 2007: Message edited by: Abdul Mohsin ]
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
init() method is called when the servlet has been instantiated but normally
when the user makes first request. Throughout the whole servlet life cycle
init() is called only once.

It is too early to do some initialization work while instantiation of servlet is done. Instead we leave those things to be done inside the init()
method.

You can do that things inside the constructor also but why to allot
resources earlier. Leave that jobs to be done inside the init() method.


the init() method performs two types of initializations:
1- General initialization
->Setting the database connection pools for requests
->Loading a datafile into HashMap
init() also have info about the page modification time expressed in miliseconds that is used by getLastModified() method.

2- Initialization controlled by the initialization parameters.
In this you can think like we developer change the servlet by changing the
code. In the same way user can make changes providing data to an HTML form.
But if you think about deployers. Providing deplorers the capability to
make changes without making changes in the servlet is the capability of
init();

DD parts role in this.



Thanks,
 
Abdul Mohsin
Ranch Hand
Posts: 111
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Chandra,

It is too early to do some initialization work while instantiation of servlet is done. Instead we leave those things to be done inside the init()
method.



But normally container first instantiate the Servlet by calling constructor and immediately calls the "init" method( tell me if I am wrong) then why its too early if we put the initialization work of "init" method in the constructor itself.?
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Abdul,

It is not always true that after constructing the object container
immediately calls the init() method. init() is called on the first
request of the client.

Although you can set the init() to be called when the servlet is loaded.
But generally the prior one is done (calling init() on first client request). That is why it is some work is left to be done inside init(),
instead of inside servlet constructor.



Thanks,
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One more thing, after the instantiation is done, you do have mere an object.
After that container makes it a servlet making a ServletConfig and calling its
init() method.

To be a servlet the object needs to be granted servletness.
From HFSJ:

There is nothing that can't wait until init().


Thanks,
[ June 06, 2007: Message edited by: Chandra Bhatt ]
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need ServletConfig which is passed to init() method. This is not available in constructor. and you know well how important the ServletConfig object is.


Ghufran
 
Abdul Mohsin
Ranch Hand
Posts: 111
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Chandra to correct me.
Now its very clear to me.
 
Abdul Mohsin
Ranch Hand
Posts: 111
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A new reason for implementing init() method and not constructor.
Direct from the source: [ UD: removed link to copyrighted material ]

The init() method is typically used to perform servlet initialization--creating or loading objects that are used by the servlet in the handling of its requests. Why not use a constructor instead? Well, in JDK 1.0 (for which servlets were originally written), constructors for dynamically loaded Java classes (such as servlets) couldn't accept arguments. So, in order to provide a new servlet any information about itself and its environment, a server had to call a servlet's init() method and pass along an object that implements the ServletConfig interface. Also, Java doesn't allow interfaces to declare constructors. This means that the


[ August 03, 2007: Message edited by: Ulf Dittmer ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic