如何把Tomcat的日志实时输出到Web页面上

2025-05-12 08:15:47
推荐回答(3个)
回答1:

这个很单,你写个servlet,去读取这个txt文件
然后在web界面上不停的ajax请求
请求到servlet后去读取这个文本的内容,不停的输出就实现了你要的效果了

回答2:

方法:前端写一个js定时器,不断的发ajax请求到后台,每回取出一段日志。后台取日志可以直接调用系统命令,或者直接调取shell脚本,取日志,判断日志文件是否存在,是否为空,返回数据的起始位置等,都可以交给shell来做。

回答3:


    


    <%
        StringBuilder sb = new StringBuilder();
        String path = System.getProperty("user.dir").replace("bin", "logs");
        String fileName = path + "\\localhost_access_log.".txt";

        File file = new File(fileName);
        if (file.exists()) {
            InputStreamReader isr = new InputStreamReader(new FileInputStream(file), "UTF-8");
            BufferedReader read = new BufferedReader(isr);


            String str = "";
            while ((str = read.readLine()) != null) {
     
                    sb.insert(0,"" + str + "");
  
            }
            out.print(sb.toString());
        } else {
            out.println("文件不存在!");
        }

    %>