如何通过HTTP请求在HTML页面中显示异步结果? [英] How could I display the asynchronous result in HTML page through a HTTP request?

查看:83
本文介绍了如何通过HTTP请求在HTML页面中显示异步结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发基于netty版本4的分析系统和基于tomcat的Web客户端应用程序.

I am developing an analysis system based on netty version 4, and a web client application based on tomcat.

Web应用程序接受用户输入,然后将其发送到分析机.最后在HTML页面中打印响应.

The web app accepts user's input then send it to analysis machine. Finally prints the response in the HTML page.

这是servlet中将用户输入发送到分析节点的代码:

This is code in a servlet of sending user's input to analysis node :

protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
String input = request.getParameter("input");
Channel ch = pool.borrowObject();
Protocol.Builder builder = Protocol.newBuilder().setInput(input);
ch.writeAndFlush(builder.build());
}

Netty异步工作,因此http请求在发送后结束.

Netty works asynchronously, so the http request ends after sending.

这是响应处理程序中的代码:

This is code in response handler:

@Override
    protected void channelRead0(ChannelHandlerContext ctx, Protocol response)
            throws Exception {
pool.returnObject(ctx.channel());
//How could I code here to display response to the HTML page that user requested?
}

我在这里苦苦挣扎了几个星期.我试图使用公共的thead-safe队列来使http请求在那里等待,直到得到队列的响应.但这使整个请求变得同步.

I have been struggling here for some weeks. I tried to use a public thead-safe queue to make http request waiting there until got the response from queue. But that made the whole request become synchronous.

谁能告诉我该怎么做?非常感谢!

Could anyone tell how to do on this ? Many thanks!

推荐答案

此处的问题是默认servlet请求生命周期是同步的.要在servlet容器中解决此问题的唯一方法是利用Servlet 3.0异步请求处理.这样,您可以将请求传递给Netty并返回,然后在响应到达时异步编写响应.

The issue here is that the default servlet request lifecycle is synchronous. The only way you're going to solve this within a servlet container is to make use of servlet 3.0 asynchronous request processing. This way you can pass the request to Netty and return, then write the response asynchronously when it arrives.

一些帮助您入门的链接

Java Servlet 3.0异步支持

如何使用异步Servlet来提高性能

这篇关于如何通过HTTP请求在HTML页面中显示异步结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆