Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

jsp

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?

Thanks. koushik

like image 614
user778900 Avatar asked Feb 24 '23 16:02

user778900


1 Answers

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

<%@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 \n with <br/>.

If the file is not saved in public webcontent, then create a servlet which gets an InputStream of it by FileInputStream and writes it to the OutputStream of the HttpServletResponse so that you can finally use <c:import> for this.

like image 62
BalusC Avatar answered Mar 07 '23 15:03

BalusC