• 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

Object instantiation in service method

 
Ranch Hand
Posts: 205
Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a servlet with following service method.



Inside service method i have instantiated the class A and class B. Hence the objects would be created on heap.

Servlet in general are Multi threaded. So if around 100 concurrent users access this servlet, would there be 100 objects on heap.

Please let me know if my analysis is correct. How to write better program in this scenario.

Please suggest.
 
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why you want to override service method?
Generally it is not adviced to override service method. whatever code one wants to write should be written in dGet/doPost methods.

Answer to your question:-

Is depends upon how you want to use class A and B.
If the need is such that each request should have their own copy of A and B objects then you have to write the code of object creation in doGet/doPost method otherwise if the A and B objects can be shared by different threads then you can use sessions or context.
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Eshwar Prasad wrote:Please let me know if my analysis is correct. How to write better program in this scenario.


Yes. Better in what way?
 
Anything worth doing well is worth doing poorly first. Just look at this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic