aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes init and constructor in servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "init and constructor in servlet" Watch "init and constructor in servlet" New topic
Author

init and constructor in servlet

Swapnil Sanghai
Ranch Hand

Joined: Dec 05, 2011
Posts: 41

Why there is no constructor for initialization of servlet? And what is the advantage of the init() over that of the constructor?
Karn Kumar
Ranch Hand

Joined: Aug 06, 2009
Posts: 146

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

Joined: Dec 05, 2011
Posts: 41

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?
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
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

Joined: Dec 05, 2011
Posts: 41

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
Rancher

Joined: Sep 21, 2011
Posts: 2408
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

Joined: Dec 05, 2011
Posts: 41

Thanks again for reply.

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


but what is reason for that?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18991
    
    8

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

Joined: Dec 05, 2011
Posts: 41

Thanks for reply.
But to be frank I didn't got it much. Can you please explain it in more detailed format?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18991
    
    8

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

Joined: Dec 05, 2011
Posts: 41

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
Rancher

Joined: Sep 21, 2011
Posts: 2408
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.
massimiliano cattaneo
Greenhorn

Joined: Jun 30, 2011
Posts: 24

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.


[SCJP 6.0 88%] [OCPJBCD 6.0 96%] [OCPJWCD 6.0 87%]
Ashwin Sridhar
Ranch Hand

Joined: Jul 09, 2011
Posts: 272

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.


Ashwin Sridhar
SCJP | SCWCD | OCA
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
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.
Ekaterina Galkina
Greenhorn

Joined: Sep 14, 2010
Posts: 15
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

Joined: Dec 05, 2011
Posts: 41

Thanks guys.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: init and constructor in servlet