简体   繁体   English

如何在不刷新页面的情况下更新JSP文件中的输出?

[英]How to update the output in the JSP file, without refreshing the page?

I am trying to use a Servlet with my JSP file using the MVC model, to be able to continuously update the UI without refreshing the page, when the output of the doGet and doSet change. 我正在尝试使用MVC模型在我的JSP文件中使用Servlet,以便在doGet和doSet的输出发生更改时能够在不刷新页面的情况下连续更新UI。

I have tried to use a timer inside the Servlet (didn't work) and also I tried to use a timer in the JSP file but it didn't work either. 我试图在Servlet中使用一个计时器(没有用),并且我试图在JSP文件中使用一个计时器,但是它也不起作用。 I thought it would help to call 'doGet' every 2 seconds to update the output I see on the screen when I run the program. 我认为这将有助于每2秒调用一次“ doGet”以更新运行该程序时在屏幕上看到的输出。 But the only time the output changes is when i manually refresh the website. 但是输出更改的唯一时间是当我手动刷新网站时。 I expect for this easy program to be able to change and see when I run the program, between the strings "blue" and "red". 我希望这个简单的程序能够在字符串“ blue”和“ red”之间进行更改并查看运行该程序的时间。

The Servlet : Servlet:

public class Servlet2 extends HttpServlet implements Servlet {

     String msg = "blue";

   protected void processRequest(HttpServletRequest request, HttpServletResponse response)
           throws ServletException, IOException {
       response.setContentType("text/html");
       request.setAttribute("state", changeColor()); 
       request.getRequestDispatcher("/test.jsp").forward(request, response);

   }

   public String changeColor(){
       if(msg.equals("red"))
           msg = "blue";
       else msg = "red";
       return msg;
   }

   @Override
   protected void doGet(
           HttpServletRequest request, HttpServletResponse response)
           throws ServletException, IOException {

       processRequest(request, response);
   }

   @Override
   protected void doPost(
           HttpServletRequest request, HttpServletResponse response)
           throws ServletException, IOException {

       processRequest(request, response);
   }

}

And the JSP file: 和JSP文件:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html> 
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
            ID:  <%=(String)request.getAttribute("state")%><br>

    </body>
</html>

As you already found out, this is not possible. 如您所知,这是不可能的。

  1. To know why this is not possible you have to know, what happens, when you access a servlet: First after you entered " http://host/yourServlet " your browser will open a connection to the host and asks for the yourServlet part. 要知道为什么不可能这样做,您必须知道访问servlet时发生了什么:首先,在输入“ http://host/yourServlet ”后,浏览器将打开与host的连接,并要求提供yourServlet部分。
  2. The Webserver (Tomcat?) will then execute the servlet's code ( doGet ) and returns anything which is outputted from the servlet code or jsp. 然后,Web服务器(Tomcat?)将执行servlet的代码( doGet ),并返回从servlet代码或jsp输出的任何内容。
  3. Your browser receives the output and renders the page. 您的浏览器将接收输出并呈现页面。
  4. After that, the connection is closed and nothing more happens. 在那之后,连接被关闭,什么也没有发生。

To achieve your change of "red" to "blue", you may reload your page automatically by using a html header ( meta http-equiv="refresh" )/javascript ( How to reload a page using JavaScript ) or you utilize javascript to ask your servlet for the current value of state ( jQuery Ajax POST example with PHP ). 为了将“红色”更改为“蓝色”,您可以使用html标头( meta http-equiv="refresh" )/ javascript( 如何使用JavaScript重新加载页面 )自动重新加载页面,也可以使用javascript询问您的servlet state的当前值( 带有PHP的jQuery Ajax POST示例 )。

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

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