Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/381.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java RequestDispatcher转发错误版本的html_Java_Servlets_Web Applications_Requestdispatcher - Fatal编程技术网

Java RequestDispatcher转发错误版本的html

Java RequestDispatcher转发错误版本的html,java,servlets,web-applications,requestdispatcher,Java,Servlets,Web Applications,Requestdispatcher,我的申请有问题。我用Java创建了一个HTML文件,然后使用RequestDispatcher将其转发给客户端。客户端输入坐标,点击一个按钮,就会得到新的HTML。取而代之的是,当我点击时,我得到了该HTML的上一个版本(它应该根据输入而改变),然后每次我刷新时,没有任何改变。文件正在本地更新。我尝试删除缓存,状态为200 OK,但仍然显示以前版本的HTML。以下是我的Java: class CreateHtml { public void createHtml() throws IOE

我的申请有问题。我用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);
    }
class创建HTML{
public void createHtml()引发IOException{
文件f=新文件(“distance.html”);
BufferedWriter bw=新的BufferedWriter(新的文件写入程序(f));
File File=新文件(“rastojanija.txt”);
BufferedReader br=新的BufferedReader(新文件读取器(文件));
String line=br.readLine();
写(“”)
+“\r\n”);
while(行!=null){
System.out.println(“LINE”+LINE);//一切正常,它打印html中应该包含的内容
bw.write(“

”+行 +“

”; 写(“
”); line=br.readLine(); } br.close(); bw.写(“”); bw.close(); } } public void doGet(HttpServletRequest请求,HttpServletResponse响应)抛出ServletException,IOException{ response.setContentType(“text/html”); 字符串值=(字符串)请求.getParameter(“input1”); mainFunc(值); CreateHtml=新建CreateHtml(); html.createHtml(); 字符串nextHTML=“/distance.html”; RequestDispatcher=getServletContext().getRequestDispatcher(nextHTML); 转发(请求、响应); }

谢谢大家!

您可能遇到浏览器缓存问题

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

很遗憾,没有更改。您知道如何使用浏览器调试功能吗?例如,F12采用镀铬。检查网络选项卡以查看文件是否从缓存加载。状态为304未修改,它来自缓存。我该怎么做才能解决这个问题?我不知道。添加带有时间戳的GET参数从未让我失望过。