Hi,

I have an android application that requests a list of objects from a servlet. The first time I run the android application it works fine by sending the correct list.
But when I run the application for the second time, it returns the list that it has already sent plus the new list. I am not sure why this is causing an issue. I use the out.flush() method but still there is no use. Could someone please tell me what is wrong with this.
I guess the servlet has to be reset after every use so that all its variables are cleared, reset to null and then used.

Thanks,
Raqeeb

Recommended Answers

All 4 Replies

most likely. No doubt you're comitting the ultimate sin of storing everything in global (maybe even static) members of your servlet class, something that should rarely if ever be done with servlets.

you are right..I have stored them globally but I havent declared them to be static...there is no way I can use my code without declaring stuff as global
please advise

well if you 'have' to use them globally, keep in mind the way a servlet works...
the first time it's called it will initialize its global values. It does NOT get destroyed once it has completed that method call...the servlet and its values stays cached. So if you're calling it again it is currently (by the way you've coded it) adding to the already initialized value - not giving it a new value because it is still in memory.
A quick way to 'cheat' that, is inside the top of your first method being called inside the servlet, is to empty the values.
something as easy as overwriting the global variable values like this:
name="";
address="";
age=0;

of course, the best way is to not use a global variable and recode the page to pass parameters where needed, but I guess you have your reasons. :)

you are right..I have stored them globally but I havent declared them to be static...there is no way I can use my code without declaring stuff as global
please advise

There is always a way...
In general, do not declare anything globally unless you want it to be available between requests (even simulataneous requests from different clients).

Worst case scenario:
client 1 sets field, client 2 sets field to something else, client 1 reads field and resets it to null, client 2 reads field.
Now client 1 has the wrong value and client 2 most likely experiences a NullPointerException.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.