user1997656
user1997656

Reputation: 532

How to store static data in a servlet and share it between all sessions/requests?

I have a servlet that I want to serve data to a web UI. the data is stored in an xml file which I expect to be modified once in a couple of days. I want to load the xml file in the servlet once, and serve it for each request, and reload it only when I send another 'reload' request.

From what I've read, static variables in servlet is bad practice.

How can I achieve what I need?

thanks.

Upvotes: 1

Views: 7249

Answers (6)

gluckonavt
gluckonavt

Reputation: 236

You can use Singleton pattern or CDI application scoped beans depends on your environment.

And do not forget about thread-safety.

public class ServletSingleton {
    private static ServletSingleton instance = new ServletSingleton();

    //Here is your data objects
    private Object firstDataObject;
    private Object secondDataObject;

    public static Object getFirstDataObject(){
        synchronized (instance){
            return instance.firstDataObject;
        }
    }

    public static Object getSecondDataObject(){
        synchronized (instance){
            return instance.secondDataObject;
        }
    }

    public static void setFirstDataObject(Object dataObject){
        synchronized (instance){
            instance.firstDataObject = dataObject;
        }
    }

    public static void setSecondDataObject(Object dataObject){
        synchronized (instance){
            instance.secondDataObject = dataObject;
        }
    }
}

This is not familiar singleton, but it uses the same principles.

Upvotes: 2

meriton
meriton

Reputation: 70564

I've really come to dislike the term "bad practice", as it insinuates that we can assess whether something is bad irrespective of circumstances. Such is rarely the case - or put differently, if the designers of the Java language had thought static fields to be always bad, they wouldn't have included them in the language.

It's totally ok to have mutable state in a servlet - but you have to keep in mind the servlet container will use the same object instance of the servlet to (concurrently) serve all requests.

This implies that state specific to the request should not be stored in the servlet, because the different request processing threads would overwrite each other's state. In your case though, you want state to be shared across all requests, so a field in the servlet is appropriate. As the servlet is accessed by concurrent threads, you will however have to synchronize access to that mutable shared state.

Upvotes: 3

user2269148
user2269148

Reputation: 157

You can define an instance members or store the attribute into the application scope but both of them are not thread safe.

Upvotes: 0

ltebean
ltebean

Reputation: 1019

You could put the data in ServletContext, it's the context per "web application" per Java Virtual Machine(means global).

Check the usage here: http://www.javatpoint.com/servletcontext

Upvotes: 1

Dima
Dima

Reputation: 8652

just create a static variable, you dont have to do anything more

Upvotes: 0

PSR
PSR

Reputation: 40318

A servlet is created only once on webapp's startup and shared among all requests. Static or not, every class/instance variable is going to be shared among all requests/sessions. You would not like to assign request/session scoped data to them. Rather declare/assign them as methodlocal variable

SEE HERE

Upvotes: 2

Related Questions