File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Servlet Life Cycle Doubts Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlet Life Cycle Doubts" Watch "Servlet Life Cycle Doubts" New topic
Author

Servlet Life Cycle Doubts

Salman Raut
Greenhorn

Joined: Mar 31, 2009
Posts: 15
Hi All,

I am learning Servlets and JSPs. I am referring Head First Servlets and JSP book.
I have the following doubts in Servlets:

1) Servlet class has a default constructor. All the initialization (that is done when the container calls the init()) can be done in that default constructor. Why is init() required if its purpose can be achieved by the default constructor?

2) Web Container allocates one thread per request. If one client sends multiple requests, then multiple threads are created by the Container. One servlet will always give the same output for multiple requests. What is the need for creating separate threads if all of them generate the same output?

Thanks for taking the time to see my questions.

Best Regards


Best Regards
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61198
    
  66

Please be sure to take the time to compose descriptive subjects for your posts; read this for more information.

Using a title of "Servlet" in a forum completely dedicated to questions on Servlets isn't very helpful.

Please go back and change your post to add a more meaningful subject by clicking the


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61198
    
  66

Saachin Tendulkar wrote:Why is init() required if its purpose can be achieved by the default constructor?
Because the init() method's purpose can't be achieved in a constructor. The ServletConfig instance needs to be built and initialized before init() can be invoked.

What is the need for creating separate threads if all of them generate the same output?
Who said that they would generate the same output? Depending upon what is submitted the output could be radically different.
Salman Raut
Greenhorn

Joined: Mar 31, 2009
Posts: 15
Hi Bear,

Thanks for the response.

I am convinced with the second answer about multiple requests from the same client.

But I have some reservations about why a constructor cannot do init() work?
Every servlet has a ServletConfig. Why is it not possible to pass ServletConfig instance as an argument to the contructor?
Then using that instance, one can do whatever ServletConfig instance does in the init().

Regards
Saifuddin Merchant
Ranch Hand

Joined: Feb 08, 2009
Posts: 605

You said every servlet has a servlet config. Think about it ... where does it get its servlet config come from? Surely you never pass it the servlet config ...

In fact the basic question is What makes an ordinary Java class into a servlet? A servlet after all is nothing but some Java code that extends (99% of the time) the HTTPservlet class .... Really nothing special about it yet...

The point is, its the container that makes the servlet special. Its the container that gives the servlet its config. Everything the servlet can do is by the 'Power of the container'.

Where do you think the container does all this for the servlet? By using the servlet's constructor of course. So if you hijack the servlet constructor the container never gets a change to create the servlet and it never gets its config and we have pretty big disaster in servlet Land.

If you are still not convinced, think about this. Do you even construct the servlet? If not then why do you think that you should have control over its constructor ...

(For a more technically correct answer I suggest HFJS - you'll understand exactly how 'grandfather container gives a servlet is servletness'....)


Cheers - Sam.
Twisters - The new age Java Quiz || My Blog
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18566
    
    8

Saachin Tendulkar wrote:But I have some reservations about why a constructor cannot do init() work?
Every servlet has a ServletConfig. Why is it not possible to pass ServletConfig instance as an argument to the contructor?

Which constructor is that? The default constructor which you mentioned in your original post? How would you propose to pass a ServletConfig instance as a parameter to a constructor which doesn't have a parameter?

So now you're left with passing it to a constructor which does have a parameter. But this isn't the default constructor, so you would have to require every single servlet ever written to have that constructor. And since the vast majority of servlets don't need to look at the ServletConfig object, that would be a vast waste of time. Besides it's bad design to require classes to have constructors of a specific type.
Salman Raut
Greenhorn

Joined: Mar 31, 2009
Posts: 15
Thanks Sam and Paul.

I am clear about the requirement of the init().

Thank you very much for clearing my doubt.

Best Regards
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Servlet Life Cycle Doubts