File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Consructor and init methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Consructor and init methods" Watch "Consructor and init methods" New topic
Author

Consructor and init methods

vijay mahendra
Ranch Hand

Joined: Jan 01, 2007
Posts: 51
Hello .. happy new year to all.

we can use both constructor and init method in servlet.

But these both when used in real time situation


thanking you .
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 9318
    
109

Have a look at the ServletsFaq which has the question "Why do servlets have an init method? Can't we make use of the servlet constructor for initialization?"


[My Blog] [JavaRanch Journal]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60056
    
  65

"vijay java",

There aren't many rules that you need to worry about here on the Ranch, but one that we take very seriously regards the use of proper names. Please take a look at the JavaRanch Naming Policy and adjust your display name to match it.

In particular, your display name must be a first and a last name separated by a space character, and must not be obviously fictitious.

Thanks!
bear
JavaRanch Sheriff


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968

One thing you have to remember about a Servlet is that YOU, the Servlet programer, are not responsible for the lifecyle of the Servlet. So, creating, initializing, threading and destroying a Servlet are not part of your domain. So, the programmer NEVER creates the Servlet, so the programmer would never invoke a constructor of a Servlet. The only people that might call a constructor on a Servlet are the people who implement the J2EE application server, or more specifically, the web container, but as a Servlet programmer, that's none of your business, and as far as you're concerned, it never happens.

HOWEVER, the Java Gods know that you'd likely want to respond to the creation or destruction of a Servlet, so they provide an init() and destroy() method that you can place code into. They people that implement the web container promise you that sometime after creating your sevlet, and before actually puttint it into service, they'll invoke your pesky little init method. So, you can't call the constructor, but you can respond to the creation lifecycle event by placing code in the init method.

That's the way I see it.

Cheers!

-Cameron McKenzie
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Consructor and init methods
 
Similar Threads
init() or init(ServletConfig sc)?
init methods of Servlets: need clarification
How does servlet decide which init method to call
exception handling
init(servletconfig)