• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Servlet Life Cycle Doubts

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Marshal
Posts: 67447
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Bear Bibeault
Marshal
Posts: 67447
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 607
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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'....)
 
Marshal
Posts: 25805
69
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Sam and Paul.

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

Thank you very much for clearing my doubt.

Best Regards
 
Barry's not gonna like this. Barry's not gonna like this one bit. What is Barry's deal with tiny ads?
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic