Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Servlet basic question

 
jibs parap
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This may be a basic question but wanted to confirm with you..

Servlets are multi-threaded, so one thread for every user request. These days, there is only one servlet(either you write it or frameworks like provides it to you) which serves as Controller and all the requests go through it; you write 'n' number of classes(action classes, utility classes etc) as per the requirements given.

When the application is functional and gets going, I assume:

1. only one instance of servlet class is created and multiple threads which spawn from it, one per each request
2. each thread of execution creates separate objects in the heap for 'n' number of classes called thereafter (its NOT that all the threads share the same objects created for the classes called thereafter)

So given the above assumptions correct, the only advantage using the servlet is, separate servlet class instance is not created for every request, thus by saving CPU usage and memory.

Is my understanding correct?

Thanks
 
Ankit Garg
Sheriff
Posts: 9578
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
These days, there is only one servlet(either you write it or frameworks like provides it to you) which serves as Controller and all the requests go through it;


Well I didn't understand this statement. It is not necessary to create one servlet to respond to every request i.e. acting as a Controller. Frameworks like Struts use a filter mapped to /* to attain this.

1. only one instance of servlet class is created and multiple threads which spawn from it, one per each request


Correct. A new thread is created to serve a request and the service method is called in that new thread.

2. each thread of execution creates separate objects in the heap for 'n' number of classes called thereafter (its NOT that all the threads share the same objects created for the classes called thereafter)


Well you are right but let me make it more clear if you are confused

 
jibs parap
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ankit for confirming.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic