• 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

why constructor only is not enough for initializing the servlet ?

 
Greenhorn
Posts: 6
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

I want to know why constructor only is not enough for initializing servlet ? i mean to say what ever the init() method do we can put that into a constructor.
so what is the thing we can achieve from init() method but not from constructor.

thanks.
 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


We cannot write constructors with arguments in a servlet class. It will throw Exception.

If you do not intend to access servlet context and config its possible to manage your code trough constructor.
 
rajpal b songara
Greenhorn
Posts: 6
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Balaji For Quick Reply,

so from your answer may i consider that 'for just making servletconfig object,servlet API provide init(ServletConfig) method'.
no additional use at container level or any-other around it ?
 
Ranch Hand
Posts: 440
Hibernate Eclipse IDE Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

rajpal b songara wrote:Thanks Balaji For Quick Reply,

so from your answer may i consider that 'for just making servletconfig object,servlet API provide init(ServletConfig) method'.
no additional use at container level or any-other around it ?



The container uses the init() method to actually register your class as an actual Servlet . The constructor alone is not enough to achieve this. After the init() method is executed ( and no exception has occured ), the container makes the servlet object and then registers it, after registration with the container , the servlet is then ready to be used and service any request.
 
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

Saif Asif wrote:The container uses the init() method to actually register your class as an actual Servlet .


Not correct. A servlet doesn't need to have an init() method at all. It is the declarations in the deployment descriptor that "register" the servlet.

After the init() method is executed ( and no exception has occured ), the container makes the servlet object


Also not correct. How could the init() method be called if the object does not already exist? Servlets are no different from other objects in Java and a method cannot be called on an instance before that instance even exists.
 
Saif Asif
Ranch Hand
Posts: 440
Hibernate Eclipse IDE Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear wrote: A servlet doesn't need to have an init() method at all. It is the declarations in the deployment descriptor that "register" the servlet.


So you mean to say that you do not need an init() method at all to initialize a servlet ?

I quote from "Mannings Book for SCWCD"

..
The servlet is initialized after
the init() method returns
..



Now I am confused here.
 
Bear Bibeault
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

Saif Asif wrote:So you mean to say that you do not need an init() method at all to initialize a servlet ?


Overriding one of the init() methods is completely optional.

I quote from "Mannings Book for SCWCD"

..
The servlet is initialized after
the init() method returns
..


You are confusing the word initialize with created. The instance is created long before any initialization takes place. The instance cannot be created after calling methods on it.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Balaji Vankadaru wrote:We cannot write constructors with arguments in a servlet class. It will throw Exception.


Not really correct. A servlet can have any number of constructors, but if it has constructors with arguments then you must add a default constructor without arguments, because the compiler will not insert it for you. Only the default constructcor will ever be called, all others are dead weight.

If you do not intend to access servlet context and config its possible to manage your code trough constructor.


Not really correct, either. A servlet's constructor will only be called once, whereas the init and destroy methods can be called multiple times.

Simple rule of thumb: never put a constructor in a servlet.
 
Saif Asif
Ranch Hand
Posts: 440
Hibernate Eclipse IDE Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear wrote: You are confusing the word initialize with created. The instance is created long before any initialization takes place. The instance cannot be created after calling methods on it.



Thank you Bear ! Cleared my misunderstanding !
 
Bear Bibeault
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

Ulf Dittmer wrote:Simple rule of thumb: never put a constructor in a servlet.


Quoted for truth. There is no valid reason to do so.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic