File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Doubt- Init() method 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 "Doubt- Init() method" Watch "Doubt- Init() method" New topic
Author

Doubt- Init() method

Abdul Mohsin
Ranch Hand

Joined: Apr 26, 2007
Posts: 111

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 ]

Regards, Abdul Mohsin
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
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,


cmbhatt
Abdul Mohsin
Ranch Hand

Joined: Apr 26, 2007
Posts: 111

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

Joined: Feb 28, 2007
Posts: 1707
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

Joined: Feb 28, 2007
Posts: 1707
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 ]
Ghufran Ul Haq
Greenhorn

Joined: Apr 10, 2007
Posts: 28
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

Joined: Apr 26, 2007
Posts: 111

Thanks Chandra to correct me.
Now its very clear to me.
Abdul Mohsin
Ranch Hand

Joined: Apr 26, 2007
Posts: 111

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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt- Init() method
 
Similar Threads
private Constructors
What's the reason ?
Cleared SCJP 1.5 with 94% :)
Collections
Any Solution of Deadlock?