简体   繁体   English

Servlet可以有多步交互吗?

[英]Can Servlets have multi-step interactions?

Is there any way to start executing java Servlet code (specifically, in Websphere Application Server) (one session, one thread on the Servlet) and then pause to get more information from the calling client at various points? 有什么方法可以开始执行Java Servlet代码(特别是在Websphere Application Server中)(一个会话,该Servlet上有一个线程),然后暂停以从各个点从调用客户端获取更多信息? I require that the current session, and ongoing Servlet thread, not die until specified, and instead keep waiting (open) for information from the client. 我要求当前会话和进行中的Servlet线程在指定之前都不会消失,而应一直等待(打开)来自客户端的信息。

Is this kind of ongoing conversation possible? 这种正在进行的对话可能吗? Or can the Servlet call to "doPost" only be started - and then the Servlet ignores the client until it finishes? 还是只能启动对“ doPost”的Servlet调用-然后Servlet忽略客户端,直到它完成?

As suggested, I would use an object stored in session to maintain the state needed. 如建议的那样,我将使用存储在会话中的对象来维护所需的状态。 You can also modify the session on a servlet by servlet basis if you need certain actions to extend the session timeout beyond the webapp defaults using the following method in the HttpSession API: 如果您需要采取某些措施来将会话超时时间延长到Webapp默认值之外,还可以使用HttpSession API中的以下方法来逐个Servlet修改会话:

public void setMaxInactiveInterval(int interval) Specifies the time, in seconds, between client requests before the servlet container will invalidate this session. public void setMaxInactiveInterval(int interval)指定客户端请求之间servlet容器使该会话无效之前的时间(以秒为单位)。 A negative time indicates the session should never timeout. 时间为负数表示会话永不超时。

You just need to establish your logic for your object setting/retrieval from session. 您只需要为会话中的对象设置/检索建立逻辑。 Typically something like this: 通常是这样的:

HttpSession session = req.getSession();
MyBeanClass bean;
Object temp = null;

temp = session.getAttribute("myBean");
if(temp !=null) {
    bean = (MyBeanClass) temp;
} else {
    bean = new MyBeanClass();
}

// Logic

session.setAttribute("myBean", bean);

I have not done this with directly, but the underlying support is somewhat related to Jetty's continuation model and Servlet 3.0 Suspend/Resume support . 我没有直接这样做,但底层支持具有一定的相关码头的延续模型Servlet的3.0挂起/恢复支持

Web frameworks that work like the post description (actually, they are resumed across different connections) are sometimes called Continuation-Based frameworks . 像帖子描述一样工作的Web框架(实际上,它们是通过不同的连接恢复的)有时也称为基于Continuation-Based的框架 I am unsure of any such frameworks in Java (as the Java language is not conducive to such models) but there are two rather well known examples of the general principle: 我不确定Java中是否有这样的框架(因为Java语言不利于此类模型),但是有两个众所周知的通用原理示例:

  1. Seaside (for Smalltalk) and; 海边 (用于Smalltalk)和;
  2. Lift (for Scala). 提升 (对于Scala)。

Hope this was somewhat useful. 希望这是有用的。

您可以保存/更新请求之间的会话状态,当下一个请求到来时,您可以还原并继续执行您的操作。

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

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