简体   繁体   English

如何在Java Servlets之间维护状态?

[英]How can state be maintained between Java Servlets?

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. 我有一些单一责任的Servlet接受请求,完成工作,响应并完成 - 在这些情况下不需要维护状态。

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. 但是,我有“普通的旧Java对象”,它根据用户在客户端上启动的操作维护状态信息,我希望根据请求提供给我的Servlet。 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. 旁注:这些数据是暂时的(可能需要保留10分钟),而不是我想保留在数据库中的东西。

Question

I have maintained a shared instance of of an object with JSP before, but in this, case a Servlet makes more sense. 我之前使用JSP维护了一个对象的共享实例,但在这种情况下,Servlet更有意义。 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? 所以,我的问题是如何适当地管理这个维护状态的对象的生命周期,并且可以通过HTTP请求或其他一些机制在无状态Servlet之间共享它?

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. 换句话说,如果这是一个非Web应用程序,则无状态Servlet将是我将任务委托给的对象,而有状态对象将保持结果。

I have looked into ServletContext, but I don't fully understand the purpose of this to know if this is what I need. 我已经研究过ServletContext,但是我并不完全理解这个目的,知道这是否是我需要的。

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. 您存储在那里的对象 需要是 可序列化的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. 如果你使用eclipse或netbeans,代码洞察功能和javadoc应该引导你如何使用它来获得更高级的东西。

If you can keep all the servlet under the same webapp (context), you can store session into ServletContext or HttpSession. 如果可以将所有servlet保存在同一个webapp(上下文)下,则可以将会话存储到ServletContext或HttpSession中。

If you need multiple instances, ServletContext/HttpSession is not going to work. 如果您需要多个实例,ServletContext / HttpSession将不起作用。 I would suggest storing sessions in a memcached. 我建议将会话存储在memcached中。

In any case, you need to manage the timeout of session yourself. 无论如何,您需要自己管理会话超时。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM