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 Object instantiation in service method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Object instantiation in service method" Watch "Object instantiation in service method" New topic
Author

Object instantiation in service method

Eshwar Prasad
Ranch Hand

Joined: Mar 21, 2008
Posts: 202
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.
Brij Garg
Ranch Hand

Joined: Apr 29, 2008
Posts: 234
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.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

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?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Object instantiation in service method
 
Similar Threads
Thread and Heap Memory
Overridding service Method
thread safe
HttpServletRequest and response
Can I call it in init method