Java 如何将文本文件的内容显示到jsp页面中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6578292/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 07:53:42  来源:igfitidea点击:

How to display the content of a text file into jsp page

javajsp

提问by user778900

I have a web project. After executing the project it will generate a text file that will contain certain result. And in the final jsp page just contains success report. But I want to show the content of the text file in to that jsp page. What i need to do to achive this?

我有一个网络项目。执行项目后,它将生成一个包含特定结果的文本文件。并且在最终的 jsp 页面中只包含成功报告。但我想将文本文件的内容显示到该 jsp 页面中。我需要做什么来实现这一目标?

Thanks. koushik

谢谢。库希克

回答by Anantha Sharma

Scenario 1: you need to open the txt file, read content & write it to the outputstream.

场景 1:您需要打开 txt 文件,读取内容并将其写入输出流。

BufferedReader br = new InputStreamReader(new FileInputStream("<<file>>"));

String line = br.readLine();
while(line!=null){
 out.println(line);
 line = br.readLine();
}

this reads the file & write the contents to the jsp

这将读取文件并将内容写入jsp

this needs to be done in the JSP...

这需要在JSP中完成...

its also advisable to move this piece of code to a support class & use that class to retrieve the file contents.

还建议将这段代码移动到支持类并使用该类来检索文件内容。

回答by Moe Matar

Is the text file generated by your web application as a result of a user request? If so, then your action bean (or servlet) which triggered the generation of the file must have access to its content. Couldn't you set the file content as an action bean property (or servlet parameter), in which case the JSP would have access to it and can then display it?

您的 Web 应用程序是否根据用户请求生成了文本文件?如果是这样,那么触发文件生成的操作 bean(或 servlet)必须有权访问其内容。您不能将文件内容设置为操作 bean 属性(或 servlet 参数),在这种情况下,JSP 将有权访问它并显示它?

回答by adatapost

You can use JSTL importtag. It imports the content of a URL-based resource.

您可以使用 JSTL导入标记。它导入基于 URL 的资源的内容。

<c:import var="data" 
          url="http://www.example.com/file.txt"
          scope="session"/>

<c:out value="${data}"/>

回答by BalusC

If the file is saved in public webcontent, then use JSTL<c:import>to display it.

如果文件保存在公共网页内容中,则使用JSTL<c:import>显示它。

<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
...
<pre><c:import url="file.txt" /></pre>

The <pre>tag is necessary so that newlines are been preserved in HTML output. Alternatives are adding CSS white-space: pre;to the containing element, or replacing \nwith <br/>.

<pre>标记是必需的,以便在 HTML 输出中保留换行符。替代方案是将 CSS 添加white-space: pre;到包含元素,或替换\n<br/>.

If the file is not saved in public webcontent, then create a servlet which gets an InputStreamof it by FileInputStreamand writes it to the OutputStreamof the HttpServletResponseso that you can finally use <c:import>for this.

如果该文件未保存在公共 webcontent 中,则创建一个 servlet,该 servlet 获取InputStreamFileInputStream并将其写入OutputStream的 ,HttpServletResponse以便您最终可以使用<c:import>它。