Alex Rivera | Logout

How can I share a variable or object between two or more Servlets?

Asked 2008-09-23T20:29:55.333
44

I would like to know if there is some way to share a variable or an object between two or more Servlets, I mean some "standard" way. I suppose that this is not a good practice but is a easier way to build a prototype.

I don't know if it depends on the technologies used, but I'll use Tomcat 5.5


I want to share a Vector of objects of a simple class (just public attributes, strings, ints, etc). My intention is to have a static data like in a DB, obviously it will be lost when the Tomcat is stopped. (it's just for Testing)

Edit
Report

1 Answer

1

Couldn't you just put the object in the HttpSession and then refer to it by its attribute name in each of the servlets?

e.g:

getSession().setAttribute("thing", object);

...then in another servlet:

Object obj = getSession.getAttribute("thing");
answered 2008-09-23T20:38:02.253

Your Answer