• 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

init and constructor in servlet

 
Ranch Hand
Posts: 52
Oracle Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why there is no constructor for initialization of servlet? And what is the advantage of the init() over that of the constructor?
 
Ranch Hand
Posts: 153
Eclipse IDE Tomcat Server Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think default constructor is provided by the container which will be used for the creation of single instance of the servlet and if you want to explicit write your own constructor then it should be public and no argument . we can't write constructor with argument.

init() method is typically used to perform servlet initialization--creating or loading objects that are used by the servlet in the handling of its requests



http://www.jguru.com/faq/view.jsp?EID=1307285

Any corrections are highly appreciated.

Thanks ..
 
Swapnil Sanghai
Ranch Hand
Posts: 52
Oracle Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks. But again we can use the constructor instead of init, for what we use init. Will it make any difference? Again why we can't pass any argument to constructor?
 
Saloon Keeper
Posts: 7582
176
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The constructor is only called once, where as the init and destory methods are called each time the servlet is put into service or taken out of service. If the servlet is programmed correctly, then the destroy method should undo (release) everything the init method did (allocated).
 
Swapnil Sanghai
Ranch Hand
Posts: 52
Oracle Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for reply. But upto my knowledge the init and destroy are also called once then.......and what about the constructor with argument?
 
Tim Moores
Saloon Keeper
Posts: 7582
176
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

But upto my knowledge the init and destroy are also called once


The servlet container is free to put servlets into service and take them out of service whenever it chooses; that's what servlet spec says. So they can be called more than once.

and what about the constructor with argument?


It can't ever be used, so there's no point in having one.
 
Swapnil Sanghai
Ranch Hand
Posts: 52
Oracle Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks again for reply.

It can't ever be used, so there's no point in having one


but what is reason for that?
 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, obviously because the container will never use a constructor with arguments. It's your turn to say something sensible now: so explain why you think it is possible the container could use such a constructor.
 
Swapnil Sanghai
Ranch Hand
Posts: 52
Oracle Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for reply.
But to be frank I didn't got it much. Can you please explain it in more detailed format?
 
Paul Clapham
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The "more detailed format" you're asking about is the Servlet Specification. I suggest you read through that document -- since you're preparing for this certification exam you should be in the habit of studying the specifications, right? -- and see what it says about construction of servlet instances.
 
Swapnil Sanghai
Ranch Hand
Posts: 52
Oracle Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am not asking about specification. I have gone through it but what exact error or exception(anything else to that I might be unaware of) that it gives when we create constructor with arguments that I am not getting.
 
Tim Moores
Saloon Keeper
Posts: 7582
176
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's easy enough to test what happens if your servlet only has a constructor that takes arguments. I think it's safe to say that if you don't know why a servlet container can't use such a constructor, then you have indeed not spent enough time reading the servlet spec, and, just as importantly, not spent enough time developing servlets.
 
Greenhorn
Posts: 24
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
Servlets are java classes like a normal classes.
So You have a constructor to make a new Instance of the new Servlet class.
To become a Servlet object You need the Init method.
The container call the init method and make other think like Create a SerletConfig object, used for example to access the init parameter in the web.xml, and so on.

I hope this can help.

 
Ranch Hand
Posts: 277
Oracle Spring Flex
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In order to intialise ServletConfig and ServletContext, the constructor is too early in the life cycle of Servlet. Init method is called only once during the life cycle of servlet.
 
Tim Moores
Saloon Keeper
Posts: 7582
176
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

massimiliano cattaneo wrote:So You have a constructor to make a new Instance of the new Servlet class.


Yes, but no initialization should take place in the constructor. So it's better not to have a constructor at all, and thus have the compiler insert the default no-arg constructor.
 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually it's not you, but servlet container who creates a servlet and calls init(). Servlet container is created in such way, that it calls init() (not constructor) for initialization. Servlet container calls constructor, but not for initialization purpose. Your question is for servlet container developer.
 
Swapnil Sanghai
Ranch Hand
Posts: 52
Oracle Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks guys.
reply
    Bookmark Topic Watch Topic
  • New Topic