Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why we need init method in servlet

 
kumar kotha
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13058
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok,
but we are having the init() method without arguments what about
that
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
kumar kotha
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in place of not u assume no
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64708
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Muhammad Saifuddin
Ranch Hand
Posts: 1324
Android Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13058
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good answers
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic