The only thing that's like a global in Java is a static variable (static final usually, but in this case just static).

If you had a class like so:

public class userInfo {
  private static String userID;
  public static void setUserID(String userID) {
    this.userID = userID;
  }
  public static String getUserID() {
    return this.userID;
  }
}

Then you could, AFTER you've set the userID yourself, treat this like a global. No need to pass the userID around.

Note of course that the above is not thread-safe, and therefore is a Very Bad Thing(tm) in servlet programming. You could pull something like this off by synchronizing the getter and setter, but of course you can only ever have one of these, so probably you'd want to instead have a class that looks very much like the above, with the synchronized methods, but the String becomes a HashMap, and in it you store userInfo object, keyed by the userID probably. This wouldn't really solve your problem because you'd have to know the userID to look up anything else, but I'm just trying to illustrate a point.

As Wendy correctly said though, just passing it around is the right answer. I suspect that's the end of the story :)

--
Frank W. Zammetti
Founder and Chief Software Architect
Omnytex Technologies
http://www.omnytex.com

Wendy Smoak wrote:
From: "Jim Douglas" <[EMAIL PROTECTED]>

I simply want to access an UserID from anywhere in an web-app.
JSP, method of any class file, servlet or otherwise...etc.
( Passing a string is what I ended up with.)


It sounds like you want a global variable like you might have in a procedural language. That would break encapsulation and go against "the rules" of object oriented programming.

In a JSP or a Servlet, you can get a reference to the session and retrieve the UserID (assuming you placed it as a session attribute.)

But it's not going to be visible to any method of any class, and IMO should not be. I think you've arrived at the correct solution-- pass the String into the method that needs it.





---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Reply via email to