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 Ajax callback doesn't initiate the servlet variable? 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 "Ajax callback doesn Watch "Ajax callback doesn New topic
Author

Ajax callback doesn't initiate the servlet variable?

Colin Zheng
Greenhorn

Joined: Oct 25, 2011
Posts: 15
Here I have JS code to call back the servlet:

...


Everytime when it sends the GET http request to the servlet, I wonder why it doesn't initiate the servlet variable "counter"? instead, the "counter" variable keeps increasing:



Doesn't the servlet 'new' itself everytime when there is a http request to it?

Thank you very much.

Colin
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60789
    
  65

Colin Zheng wrote:Doesn't the servlet 'new' itself everytime when there is a http request to it?

No.

And there is your answer (which has nothing at all to do with Ajax).


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Colin Zheng
Greenhorn

Joined: Oct 25, 2011
Posts: 15
Thanks for the quick answer! Can you shed more lights on how the servlet initiates itself upon a http request? From another http client, the servlet definitely 'new' itself, right? I thought the http request is a stateless protocol.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60789
    
  65

The fact that HTTP is stateless has nothing at all to do with the servlet life-cycle.

The container will load a servlet into memory before or on the first request to it. That single instance will be used to serve all requests to that servlet. This is why it is important to write servlets in a thread-safe manner. (Which yours is not, by the way.)

The servlet will be taken out of service by the container, usually when its context is being removed from service.

There are nuances to this, but that's the general life cycle of a servlet.
Colin Zheng
Greenhorn

Joined: Oct 25, 2011
Posts: 15
Thanks you so much. Yes, I just proved that another client will affect the variable 'counter' so it's not thread safe. I have to learn more on how to make it thread safe. Moving on now...
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60789
    
  65

Thread-safe rule #1: no instance variables.
Colin Zheng
Greenhorn

Joined: Oct 25, 2011
Posts: 15
But from the same client, I do want to track an event progress that is happening on the server side, large file uploading for example. I guess there is a lot more for me to learn.
 
jQuery in Action, 2nd edition
 
subject: Ajax callback doesn't initiate the servlet variable?
 
Similar Threads
Displaying a Greeting message using Ajax
passing xml to jsp/java
How to insert at runtime using AJAX
Loading xml in Javascript fails
Problem with String compare in JavaScript