ABC
ABC

Reputation:

is there a way to share a single variable by all servlet classes in java

i need to know whether there is a way to change the value of a single variable by two servlet classes. in one servlet i need to make a variable =true and in another servlet i need to make the variable =false

Upvotes: 0

Views: 760

Answers (4)

Tom Hawtin - tackline
Tom Hawtin - tackline

Reputation: 147164

Stick the data in your database (or other persistent store).

There is the application context, but as Michael Borgwardt points out in his answer, that isn't actually global.

Upvotes: 0

Kyle G
Kyle G

Reputation: 467

If you are want to store it you could use a HTTPSession and then any of the servlets will be able to access the variable.

HttpSession session = request.getSession();
session.setAttribute("hello", "test");

http://www.exforsys.com/tutorials/jsp/jsp-session-object-methods.html

Take a look at that page, some basic info on sessions.

Upvotes: 0

Michael Borgwardt
Michael Borgwardt

Reputation: 346546

If you want a global variable for the entire web app, that is what attributes in the ServletContext are for - be sure to read the doc carefully so you understand how "global" these attributes are exactly.

If you want the variable to be individual per user, but global across servlets, that's what attributes in the HttpSession are for.

Upvotes: 4

Marko
Marko

Reputation: 31493

What do you want exactly?

You can use singleton and keep that var there, or use session or request or whatever...

Upvotes: -2

Related Questions