requestdispatcher转发了错误的html版本

wvmv3b1j  于 2021-06-29  发布在  Java
关注(0)|答案(1)|浏览(291)

我的申请有问题。我用java创建一个html文件,然后使用requestdispatcher将其转发给客户机。客户端输入坐标,点击一个按钮,就会得到新的html。取而代之的是,当我点击时,我得到了该html的前一个版本(它应该根据输入而改变),然后每次我刷新时,什么都没有改变。文件正在本地更新。我尝试删除缓存,状态是200 ok,但仍然显示以前版本的html。以下是我的java:

class CreateHtml {
    public void createHtml() throws IOException {
        File f = new File("distance.html"); 

        BufferedWriter bw = new BufferedWriter(new FileWriter(f));

        File file = new File("rastojanija.txt");
        BufferedReader br = new BufferedReader(new FileReader(file));
        String line = br.readLine();

        bw.write("<html><body style=\"height: 97%;"
                + "background-image: url(slikiApp/pozadina_mapa_burred.jpg); background-size: cover; background-repeat: no-repeat;"
                + "font-family: 'Trebuchet MS', sans-serif\">"
                + "<a href=\"home.html\"><img src=\"slikiApp/levo_strelka.png\" style=\"float:left; height:15%; width:10%; cursor:pointer\"></a><img style=\"display:block; margin-left:auto; margin-right:auto; width: 30%\" id=\"logoDistance\" src=\"slikiApp/logo_shadow.png\"><div id=\"distanceContainer\""
                + "style=\"margin:auto; margin:auto; height:65%; width:50%; padding:10px\" id=\"distanceContainer\">\r\n");

        while (line != null) {
            System.out.println("LINE"+line); //here is everything ok, it prints what should be in the html
            bw.write("<p style=\"font-size:3vw; margin-left:10px\">" + line
                    + "</p><div style=\"margin-top: -95px; margin-left:475px\"><img onclick=\"addToVisited()\" style=\"height:16%; width:26%; cursor:pointer\" src=\"slikiApp/pin_shadow.png\"><img onclick=\"addToFaves()\" style=\"height:16%; width:37%; cursor:pointer\" src=\"slikiApp/favourites.png\"></div>");
            bw.write("<br>");
            line = br.readLine();
        }
        br.close();
        bw.write("</div></body></html>");

        bw.close();
    }
}

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html");
        String value = (String) request.getParameter("input1");

        mainFunc(value);

        CreateHtml html = new CreateHtml();
        html.createHtml();

        String nextHTML = "/distance.html";
        RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(nextHTML);
        dispatcher.forward(request, response);
    }

谢谢您!

2guxujil

2guxujil1#

您可能遇到浏览器缓存问题。
您可以直接将html流式传输到响应,而不是转发
如果确实需要写入该文件并将客户端转发给它,可以尝试通过添加一个伪get参数来“破坏”浏览器缓存逻辑,例如转发到 "/distance.html?t=" + System.currentTimeMillis()

相关问题