tags:

views:

260

answers:

4

hi,

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

A: 

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.

Kyle G
how can i do that
See Brian Goetz'z article on why HttpSession is usually used incorrectly. http://www.ibm.com/developerworks/library/j-jtp09238.html
Tom Hawtin - tackline
A: 

What do you want exactly?

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

Dev er dev
+3  A: 

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.

Michael Borgwardt
A: 

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.

Tom Hawtin - tackline