Help coderanch get a
new server
by contributing to the fundraiser
  • 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

Servlet Constructor

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the performance impact of defining a no-argument constructor of my servlet?
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since a servlet instance stays in service for extended periods, the constructor time is not significant.
Bill
 
Sudipto Ghosh
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot for the quick response ...
What happens when the servlet is loaded for the first time ? I know that we may not define a constructor as we have init() method where we can put the desired behavior. But I heard that it really affects the performance and basically that is the reason it is not recommended to define servlet constructor.
Please enlighten me !!!
 
Ranch Hand
Posts: 783
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As far as performance, I can't imagine that it would make a difference...

The biggest reason not to define a servlet constructor (that I am aware of) is that the object isn't truly a servlet until the container initializes it by calling the init() method. Basically, when the servlet constructor is called, an object is created just like any other java object. But it cannot yet do all the things that we expect a servlet to do because it does not have access to things like the context or config objects. It receives these from the container when it is initialized.
 
William Brogden
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

But I heard that it really affects the performance and basically that is the reason it is not recommended to define servlet constructor.


There are a lot of bizarre ideas about what affects servlet performance floating around - where do these things come from
Whoever is spreading them is doing a great disservice.
The first time a JSP is accessed, the compilation takes a (relatively) long time but the constructor execution takes no more time than constructing any other Java object.
Bill
 
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think a lot of this misinformation is coming from people with some experience in CGI and maybe a bit of Java knowledge but no knowledge whatsoever of how Servlet engines work.

They directly translate traditional CGI principles to Java and think deduce the working of a Servlet engine based on that, which is of course quite incorrect.
If indeed a Servlet engine were to work like a traditional CGI engine it would start a new Servlet instance for each request and throw it away after the request were done. Of course in actual Servlet engines this does not happen and in fact only a single Servlet instance is created either during engine startup or on receiving the first request for that Servlet and then it is used in a multithreaded fashion obviating the need to heavy object construction (and even heavier process construction) which a traditional CGI system would have to go through.
 
William Brogden
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I think a lot of this misinformation is coming from people with some experience in CGI and maybe a bit of Java knowledge but no knowledge whatsoever of how Servlet engines work.


AHA! That makes sense! I bet you are right!
Bill
 
Sudipto Ghosh
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Referring to my question, I heard that there is a loop that goes on between the init() method call and the invocation of the servlet constructor - basically that is the reason one should not go on for defining a servet constructor. This was just a headsup to you.
However, I have tried it in Tomcat and it is behaving just like normal.
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I heard that there is a loop that goes on between the init() method call and the invocation of the servlet constructor



You got some bad information.
 
William Brogden
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I heard that there is a loop that goes on between the init() method call and the invocation of the servlet constructor


Is there somebody somewhere pretending to be a servlets API Guru and teaching this stuff? We do seem to get a few of these every week.
Bill
 
That which doesn't kill us makes us stronger. I think a piece of pie wouldn't kill me. Tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic