This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Servlets and the fly likes How does servlet stay active ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "How does servlet stay active ?" Watch "How does servlet stay active ?" New topic
Author

How does servlet stay active ?

jignesh soni
Ranch Hand

Joined: Dec 10, 2007
Posts: 150
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

Joined: Jan 10, 2002
Posts: 60794
    
  65

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
John Kimball
Ranch Hand

Joined: Apr 13, 2009
Posts: 96
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

Joined: Feb 22, 2009
Posts: 192
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

Joined: Nov 20, 2008
Posts: 254
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.


“Everything should be as simple as it is, but not simpler.” Albert Einstein
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60794
    
  65

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

Joined: Nov 20, 2008
Posts: 254
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

Joined: Jan 10, 2002
Posts: 60794
    
  65

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

Joined: Feb 22, 2009
Posts: 192
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

Joined: Nov 20, 2008
Posts: 254
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

Joined: Jan 10, 2002
Posts: 60794
    
  65

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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How does servlet stay active ?
 
Similar Threads
Doubt regarding java compilation
instance of server
Servlet Filter question
To run a JSP in a fixed interval
jvm scope