• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How does servlet stay active ?

 
jignesh soni
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Every CGI request requires a new process to start the execution of program. That is everytime a request is executed, that program exits from the server.

In case of servlet, it keeps running even after a request is executed. How does servlet keep running after a request is executed ? Whats the mechanism that keeps it running ?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64718
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Depends what you mean by "running". No, there is no code executing when there is no request to respond to. Yes, the servlet stays loaded into memory.

Remember, a servlet is a Java class that is part of a larger Java program (the container) that stays running on the server.
 
John Kimball
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's the magic of app servers: You have one JVM process that stays alive--I think it's one per Web Application?--and that one process automatically spawns threads as needed, to serve the different requests.

You, as the developer, don't have to write an ounce of thread-creation code because the app server does it for you. In fact, if your servlet doesn't have any member variables or share objects across the application you wouldn't even have to think about concurrency.

Except many servlets do evolve to share something, which is when things get ugly pretty quickly and coders have to tread far more cautiously.






 
Ryan Beckett
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is only ever one instance of a servlet running in memory. Per request, the Container creates a new thread with its own stack, appends the request and response objects to it, and runs the servlet's service method. Once the method has completed, the response object is sent to the client and the thread is destroyed.
 
Duc Vo
Ranch Hand
Posts: 254
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ryan Beckett wrote:There is only ever one instance of a servlet running in memory.

Sorry for going out of topic a little. But don't assume this though. The only thing you may need considering is that servlet attributes are not thread safe.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64718
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Indeed. There are circumstances where more than one instance of a servlet can be loaded, but that has no bearing on how Servlets need to be written in a thread-safe manner.
 
Duc Vo
Ranch Hand
Posts: 254
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:Indeed. There are circumstances where more than one instance of a servlet can be loaded, but that has no bearing on how Servlets need to be written in a thread-safe manner.

If you don't use servlet attributes and/or only use final attributes then you don't have threading issue. But if you do use servlet attributes and manipulate these attributes within the service method then you do have to worry or should consider changing your design.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64718
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Duc Vo wrote:If you don't use servlet attributes and/or only use final attributes then you don't have threading issue.

Huh? What are "servlet attributes"?

If you are talking about instance variables, then yes, that's my entire point. Servlets must be written in a thread-safe manner.
 
Ryan Beckett
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am well aware that only local variables and request attributes are thread safe. What is a scenario that would allow multiple instances of a servlet to be loaded?
 
Duc Vo
Ranch Hand
Posts: 254
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ryan Beckett wrote:I am well aware that only local variables and request attributes are thread safe. What is a scenario that would allow multiple instances of a servlet to be loaded?

Say, in a cluster environment.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64718
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ryan Beckett wrote:I am well aware that only local variables and request attributes are thread safe. What is a scenario that would allow multiple instances of a servlet to be loaded?

The same servlet class can be declared multiple times in the deployment descriptor, and each will cause a servlet instance to be loaded. Additionally, the servlet specification allows for the container to decide to instantiate as many instances as it would like based upon its internal implementation details.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic