On Fri, 18 Jun 1999, Andras Balogh wrote:

>         Hi !
>
>     I was wondering it is possible  to have one global variable (a
> String object or an int) that can be accessed  from all servets on
> the same webserver ?
>    I know a line of a file or a table field from a database could be
> a "global variable" but than I a need connection in every servlet.
> Could it be done in a more simple way?

How about using the Singleton pattern?  Some skeleton code:

public class MyGlobalVar {
  private MyGlobalVar instance = null;
  // whatever other member data fields needed

  private MyGlobalVar(...) {
    // do whatever is needed to initialize MyGlobalVar
  }

  public static MyGlobalVar getInstance() {
    if (instance == null) instance = new MyGlobalVar(...);

    return instance;
  }

  // whatever other memeber methods needed
  // most likely read-only methods
}

I think I got everything right there.

Milt Epstein
Research Programmer
Software/Systems Development Group
Computing and Communications Services Office (CCSO)
University of Illinois at Urbana-Champaign (UIUC)
[EMAIL PROTECTED]

___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".

Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html

Reply via email to