简体   繁体   English

如果要显示新信息,如何使servlet刷新jsp页面?

[英]how to make a servlet refresh a jsp page if there is new information to be displayed?

I am making a web application that receives pictures from an android smartphone and then displayes them in a .jsp page. 我正在制作一个Web应用程序,该应用程序从Android智能手机接收图片,然后将其显示在.jsp页面中。

I need a mechanism to reload the .jsp page if there is new pictures in the server. 如果服务器中有新图片,我需要一种机制来重新加载.jsp页面。 Kinda like facebook where we dont need to refresh to see new content. 有点像facebook,我们不需要刷新即可查看新内容。

Does facebook just refreshs from time to time or there is some mechanism that realizes there is new information to be displayed and notifies the page to refresh itself? 是不是Facebook只是不时刷新,还是有某种机制意识到有新信息要显示并通知页面进行刷新?

The servlet only informs that there is new information then, the jsp refreshes itself. servlet仅通知有新信息,然后jsp刷新。 The other option is to use AJAX so that the servlet returns you the new information let's say in a JSON format. 另一个选择是使用AJAX,以便servlet以JSON格式返回新信息。 And then by using javascript you update your webpage with the new information (and without refreshing the page because of AJAX). 然后,通过使用javascript,您可以使用新信息来更新网页(并且由于AJAX而不刷新页面)。

As the other folks answered, with HTTP the client (web browser) must initiate the message exchange. 正如其他人回答的那样,使用HTTP,客户端(Web浏览器)必须启动消息交换。 In other words. 换一种说法。 the server (JSP is a server process) cannot send a message to a web browser. 服务器(JSP是服务器进程)无法将消息发送到Web浏览器。 The web browser must start the process (by sending a request). Web浏览器必须开始该过程(通过发送请求)。 The server can only respond the web browser's request. 服务器只能响应Web浏览器的请求。

So as the other folks pointed out. 因此,正如其他人指出的那样。 Face book probably sends a javascript program to the web browser. 脸书可能会将javascript程序发送到Web浏览器。 The javascript program running in the web browser every few seconds sends a message to server. 每隔几秒钟在网络浏览器中运行的javascript程序就会向服务器发送一条消息。 The message asks the server whether there is new information to display. 该消息询问服务器是否有新信息要显示。

Hope that makes sense. 希望有道理。 You need to add javascript that runs in the browser. 您需要添加在浏览器中运行的javascript。 That javascript runs in a loop, polling the server for new info. 该javascript循环运行,轮询服务器以获取新信息。 The server does not poll the web browser. 服务器不会轮询Web浏览器。 It's the other way around. 相反。 The web browser polls the server. Web浏览器将轮询服务器。

Hope that helps :) Good Luck. 希望能对您有所帮助:)祝您好运。

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

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