java 如何从服务器读取文本文件并在 index.jsp 中显示文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4868004/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to read a text file from server and display the text in index.jsp
提问by Vardges
I am woking on web service.Now I have session folders for each user, and each user has it's log file there. Now I want to read log files from java and pass it to index.jsp for show. As I have already used javax.servlet.http.HttpServletRequest req
- the req.setAttribute(REQUEST_IS_LOG, log);
and req.getRequestDispatcher("index.jsp").forward(req, res);
do not work for me. Can someone help me to find another way? How can I take the text from file in display it in index?
Are they any way to do this with ajax?
Thank you in advance!
我正在使用网络服务。现在我有每个用户的会话文件夹,每个用户都有它的日志文件。现在我想从 java 读取日志文件并将其传递给 index.jsp 进行显示。正如我已经使用过的那样javax.servlet.http.HttpServletRequest req
-req.setAttribute(REQUEST_IS_LOG, log);
并且req.getRequestDispatcher("index.jsp").forward(req, res);
不适合我。有人可以帮我找到另一种方法吗?如何将文件中的文本显示在索引中?他们有什么办法可以用ajax做到这一点吗?先感谢您!
回答by BalusC
If it's in public webcontent, just use <jsp:include>
.
如果它在公共网络内容中,只需使用<jsp:include>
.
<pre>
<jsp:include page="logs/user123.txt" />
</pre>
Otherwise bring a HttpServlet
in between which gets an InputStream
of the desired resource and writes it to the OutputStream
of the response.
否则,HttpServlet
在两者之间引入一个获取InputStream
所需资源的 并将其写入OutputStream
响应的 。
<pre>
<jsp:include page="logservlet/user123.txt" />
</pre>
Or if it is located at a different public domain, use JSTL <c:import>
.
或者,如果它位于不同的公共域,请使用 JSTL <c:import>
。
<pre>
<c:import url="http://other.com/logs/user123.txt" />
</pre>
As to the Ajax part, just do something like
至于 Ajax 部分,只需做一些类似的事情
document.getElementById("log").innerHTML = xhr.responseText;
See also my answer on this questionfor more extensive examples.
有关更广泛的示例,另请参阅我对这个问题的回答。
回答by Michael K
JSP:
JSP:
<% BufferedReader reader = new BufferedReader(new FileReader("log.txt")); %>
<% String line; %>
<% while ((line = reader.readLine()) != null) { %>
<%=line %>
<% } %>
This will work because jsp's can do anything Java can do. However, for larger projects you should look into using a Model-View-Controller implementation. There are several frameworks that can assist with this, such as Spring or Struts.
这会起作用,因为jsp 可以做Java 可以做的任何事情。但是,对于较大的项目,您应该考虑使用模型-视图-控制器实现。有几个框架可以帮助解决这个问题,例如 Spring 或 Struts。
回答by Vardges
Finally I did like:
最后我确实喜欢:
res.setContentType("text/plain");
request.setAttribute(REQUEST_IS_LOG, logs);
request.getRequestDispatcher("index.jsp").forward(req, res);
return;
Before I write like:
在我写之前:
java.io.OutputStream result=res.getOutputStream();
that was why I couldn't use the method, which I wrote above. I just change to file like:
这就是为什么我不能使用我上面写的方法。我只是更改为文件,如:
java.io.OutputStream result = new java.io.FileOutputStream((destinationDir+System.getProperty("file.separator")+"result"+n+"."+targetFormat.toLowerCase()));
and it works!
它有效!