aspose file tools*
The moose likes Servlets and the fly likes why we need init method in servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "why we need init method in servlet" Watch "why we need init method in servlet" New topic
Author

why we need init method in servlet

kumar kotha
Greenhorn

Joined: Jul 28, 2006
Posts: 6
every extended servlet having constructor for initialisation of resouces
then why we need init method servlet
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12803
    
    5
1. The servlet container needs a no-args constructor to create an instance from the class named in web.xml - therefore constructors with arguments are never used in a real servlet container - if you want to make one for testing outside a servlet container, feel free.
2. A servlet instance needs to know the ServletConfig information before it can work in a servlet container therefore there must be an init(ServletConfig conf) method.

Bill
kumar kotha
Greenhorn

Joined: Jul 28, 2006
Posts: 6
ok,
but we are having the init() method without arguments what about
that
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

There are two init methods.
The one you've listed is a convenience method that gets called by init(config).

This is why it's important to call:
super.init(config) if you override init(config).


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
kumar kotha
Greenhorn

Joined: Jul 28, 2006
Posts: 6
see my conversation from first to last
there is not init() [with out arguments ] method what will happend
already we have constructor and init(ServletConfig con)
kumar kotha
Greenhorn

Joined: Jul 28, 2006
Posts: 6
in place of not u assume no
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61419
    
  67

kumar kotha, JavaRanch is a community of people from all over the world, many of who are not native English speakers. While using abbreviations like "u" instead of spelling out "you" is convenient when text messaging your friends on a cell phone or in a chat room, it presents an extra challenge to those that are already struggling with English. Additionally, such shortcuts may confound automated translation tools that patrons of the Ranch may be making use of.

I would like to ask for your help in making the content of JavaRanch a little easier to read for everybody that visits here by not using such abbreviations.

Please read this for more information.

thanks,
bear
JavaRanch Sheriff


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Muhammad Saifuddin
Ranch Hand

Joined: Dec 06, 2005
Posts: 1321

Originally posted by kumar kotha:
in place of not u assume no


you can almost always able to edit your post. used the pencil-pad icon to do so.


Saifuddin..
[Blog][Linkedin] How To Ask Questions On JavaRanch My OpenSource
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12803
    
    5
Look at the Servlet interface in the JavaDocs - you will see that only one init method is required in a servlet, the one using ServletConfig. The servlet container ALWAYS calls this method after creating an instance and before any requests are processed.

NOW - look at the GenericServlet abstract class JavaDocs - you will see that this class has added the init() as "a convenience method" along with many other methods. The init( config ) method in GenericServlet calls the init() method - as described in the JavaDocs.

Anybody serious about servlet programming should become familiar with the JavaDocs for the servlet related packages and the formal specification of the API.

Bill
Jimi Parekh
Greenhorn

Joined: Jul 30, 2006
Posts: 7
Good answers
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: why we need init method in servlet