• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Consructor and init methods

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 .
 
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?"
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author and cow tipper
Posts: 5009
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic