This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft 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 Java 8 in Action this week in the Java 8 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: 60077
    
  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: 60077
    
  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: 60077
    
  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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Ajax callback doesn't initiate the servlet variable?
 
Similar Threads
Loading xml in Javascript fails
Problem with String compare in JavaScript
How to insert at runtime using AJAX
passing xml to jsp/java
Displaying a Greeting message using Ajax