views:

201

answers:

2

Situation

I have a few single-responsibility Servlets that take a request, do their job, respond, and are done -- no state need be maintained in these cases.

However, I have I "Plain Old Java Object" that maintains state information based on actions the user has initiated on the client that I would like to make available upon request to my Servlets. I would like to make a single instance of this object available and do not need/want to maintain multiple, shared instances.

Side Note: This data is transient (need to keep it for 10 minutes maybe) and not really something I would like to keep in a database.

Question

I have maintained a shared instance of of an object with JSP before, but in this, case a Servlet makes more sense. So, my question is how do I appropriately manage the lifetime of this object that maintains state and can share it among stateless Servlets via HTTP requests, or some other mechanism?

Put another way, if this were an non-web application, the stateless Servlets would be objects I would delegate a task to and the stateful object would maintain the results.

I have looked into ServletContext, but I don't fully understand the purpose of this to know if this is what I need.

Thank you for the help,

-bn

+10  A: 

Maybe I am understanding your question wrong, but have you thought of the session?

[edit] So you really need the session.

You can use the session for example this way:

public class TestServlet extends HttpServlet {
....
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws  ServletException, IOException {
    request.getSession().setAttribute("test", new Date());
  }
....
}

The object that you store there, needs to should be serializable IIRC.

If you use eclipse or netbeans the code-insight feature and the javadoc should lead you the way on how to use it for more advanced stuff.

Patrick Cornelissen
+1 the HttpSession is indeed what is described in this question.
Pascal Thivent
+1 for the simple answer to what you thought was a trick question
Zoidberg
+1: Yup, it sounded like a trick question, but sessions are what came to my mind while reading the question too.
R. Bemrose
I have no doubt the question I am asking is a Servlet beginner question...could you elaborate on how to use the session? By the way, I need a single instance of this object o be shared (edited above). Thanks!
bn
Thanks I just reached this conclusion, this makes much more sense now. Finally some separation between logic and view. Thanks for the help.
bn
(There is actually no requirement that objects stored in a session must be Serializable. Its an option, not a requirement.)
John O
+1  A: 

If you can keep all the servlet under the same webapp (context), you can store session into ServletContext or HttpSession.

If you need multiple instances, ServletContext/HttpSession is not going to work. I would suggest storing sessions in a memcached.

In any case, you need to manage the timeout of session yourself.

ZZ Coder
Most servlet containers should support session replication among nodes
matt b
I wouldn't recommend it. It rarely works (at least in Tomcat). In most cases, we add more servers because memory is running out. Now you got 2 servers both running out of memory ... Not even mentioning the replication lag ... memcached is much better solution.
ZZ Coder