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

Consructor and init methods

 
vijay mahendra
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?"
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64712
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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
 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic