aspose file tools*
The moose likes Servlets and the fly likes Servlet Variable persisting in Tomcat? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlet Variable persisting in Tomcat?" Watch "Servlet Variable persisting in Tomcat?" New topic
Author

Servlet Variable persisting in Tomcat?

Brian K Swingle
Ranch Hand

Joined: Jun 20, 2003
Posts: 39
Hello all,
I am dumb founded by this one. I'm having an issue with my servlets were i run an instance of the servlet on one system and set certain Strings and int's to data related to that system and then run another instance of that servlet on another system and data relating to the first system that couldn't be reproduced is displayed on the second system. How is this possible? Do i need to enact the garbage collection in in the finally? I dont have keep alive enabled in tomcat. So i'm completely confused has this happened to anyone here? Any help would be greatly appreciated.

Thanks
Brian
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60050
    
  65

By "system" do you mean separate computers? Or separate web apps on the same computer?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Brian K Swingle
Ranch Hand

Joined: Jun 20, 2003
Posts: 39
Hello Bear,
Thank you for responding i didn't think anyone would jump in on this one. Um as for your question. By system i mean clients two seperate desktops. Running seperate sessions of a servlet hosted on one server. It's odd the first desktop a sony runs the servlet that gathers some system model information then with that window still open on the sony i run the same page that calls the servlet on the dell right next to it but instead of it bringing up the dells system information it brings up the sony's system information. Do you have any idea what might be causing this?

Thanks,
Brian
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60050
    
  65

Both clients (what you call "systems") are hitting the same server, no? In that case, each client is hitting the same instance of the servlet. So I'm betting you are storing data in instance variables in the servlet?

If so, that is a major no-no. Since only one instance of a servlet is created to serve all clients, they must be written to be thread safe. And that means no instance variables (or other non-thread-safe techniques).
Brian K Swingle
Ranch Hand

Joined: Jun 20, 2003
Posts: 39
Ok I'm creating my variables like this. I believe they are only visible inside the class in terms of scope and visibility. But i dont know if this is thread safe. If not could you please show me an example of how to make this thread safe? The only way i was able to stop the data from persisting was to set all the variables to null in the finally of the doPost. Thanks for all your help.


This is how i'm creating my variables:

public class ServletName extends HttpServlet {


static String UserName = "";
static String UserEmail = "";
ch praveen
Ranch Hand

Joined: Mar 08, 2004
Posts: 67
Hello Brian K Swingle ,

Why can't you use session variables in your servlet, such as ::
....
....
HttpSession session = request.getSession(true);
session.setAttribute("userid",userid);
session.setAttribute("Email","email value")
....
....

Later you could extract values as follows::

String userid = (String)session.getValue("userid");
String email = (String)session.getValue("Email");
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60050
    
  65

Ok I'm creating my variables like this. I believe they are only visible inside the class in terms of scope and visibility. But i dont know if this is thread safe.


Most certainly not. Because the servlet instance is shared across all threads, your variables are used by all threads referencing the servlet.


If not could you please show me an example of how to make this thread safe?


Don't use instance variables. Use scoped attributes. Attributes that are only needed for the current request should be placed in request scope, attributes that need to persist across requests but are specific to the current user should be placed on the session, and attributes that need to persist but are not user-specific can be placed in application (aka servlet context) scope.

ch praveen included some examples of session-scoped variables.


The only way i was able to stop the data from persisting was to set all the variables to null in the finally of the doPost.


That won't work either. Think of what will happen if doPost gets called by two (or more) clients at the same time!
[ June 01, 2004: Message edited by: Bear Bibeault ]
Brian K Swingle
Ranch Hand

Joined: Jun 20, 2003
Posts: 39
ok i understand now. Thank you both for your help i will go through and correct this.

Thanks,
Brian
Brian K Swingle
Ranch Hand

Joined: Jun 20, 2003
Posts: 39
Sorry to bring this one from the grave but the issue is not resolved. I implemented SingleThreadModel and am creating all my variables as private in the class like so.

public class CDW_InSPECS extends HttpServlet implements SingleThreadModel{



private String Name = "";
private String Email = "";

But the values in the variables persist between requests. Now from what you have told me and from reading the only real way to be thread safe is to use session objects to store the variables values like so.

session.setAttribute("NumberOfCPU",userid);
String email = (String)session.getValue("Email");

so what i dont get is if i recieve the request and parse it out using req.getParameter("Email") and then put it into a session object then retrieve it and place it into a String what did that actually solve? Since the data ended up in a variable that could persist in the end?

Now to resolve my issue in the short term i am implementing SingleThreadModel and setting all my variables to null after the servlet completes. but that kind of seems to be an A$$ backward solution to something that shouldn't be this complicated. Now im all over the place and need some clearification. Please let me know what you think.

Best Regards,
Brian
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12675
    
    5
NO
DO NOT use SingleThreadModel
DO NOT use instance variables to hold user specific data
Working with servlets requires a major change in approach. Learn how to use the correct tool for associating variables with a given user.
That tool is the HttpSession as mentioned above.
Any tutorial will walk you through the details.
Bill


Java Resources at www.wbrogden.com
Brian K Swingle
Ranch Hand

Joined: Jun 20, 2003
Posts: 39
High All,
OK, I understand and i dont disagree i want to use the session object but i cant find a brief example of exatly how to implement that structure. Every example i have found is like 2,000 lines. Do you recommend and books that cover this subject in detail? Or do you know of an example you could point me too?

Also can anyone answer my question from before about retirieveing information from the session but only to store it in a variable anyway. Doesn't that defeat the purpose of using the session or am i missing something?

Thanks for all your help and patience.
Brian
Brian K Swingle
Ranch Hand

Joined: Jun 20, 2003
Posts: 39
In addition to that last post.

Unless creating variables in the doPost/doGet is thread safe were creating them outside of the doPost/doGet is not. Is this the answer or am i still way off track?

Thanks,
Brian
Brian K Swingle
Ranch Hand

Joined: Jun 20, 2003
Posts: 39
Ok sorry,
I found the answer to my question i was right with the assumtion in my last post. It was explained to another member in this thread.

http://www.coderanch.com/t/348790/Servlets/java/member-variables-servlet

For anyone else who might be wondering. I think i have all the info i need now thank you for all your guys help and patience.

thanks,
Brian
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Servlet Variable persisting in Tomcat?
 
Similar Threads
separate webserver and application server
out-of-process servlet
Stop Servlet
threading issue in servlet
Tomcat multithread servlet