我正在使用 Web 服务。现在我有每个用户的 session 文件夹,每个用户都有它的日志文件。现在我想从 java 中读取日志文件并将其传递给 index.jsp 进行显示。因为我已经使用了 javax.servlet.http.HttpServletRequest req
- req.setAttribute(REQUEST_IS_LOG, log);
和 req.getRequestDispatcher("index.jsp ").forward(req, res);
对我不起作用。有人可以帮我找到另一种方法吗?如何从文件中获取文本并在索引中显示?
他们有什么办法可以用ajax做到这一点吗?
提前致谢!
最佳答案
如果它在公共(public)网络内容中,只需使用 <jsp:include>
.
<pre>
<jsp:include page="logs/user123.txt" />
</pre>
否则带一个HttpServlet
在这两者之间得到一个 InputStream
所需资源并将其写入 OutputStream
的响应。
<pre>
<jsp:include page="logservlet/user123.txt" />
</pre>
或者如果它位于不同的公共(public)域,请使用 JSTL <c:import>
.
<pre>
<c:import url="http://other.com/logs/user123.txt" />
</pre>
至于 Ajax 部分,只需做类似的事情
document.getElementById("log").innerHTML = xhr.responseText;
另请参阅我在 this question 上的回答获取更广泛的示例。
关于java - 如何从服务器读取文本文件并在 index.jsp 中显示文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4868004/