0% found this document useful (0 votes)
457 views

Read and Display A Local Text File in JSP Page - Stack Overflow

The document discusses displaying a local text file in a JSP page. The original code used System.out.println() to print the file contents, but this sent it to the server log rather than displaying in the browser. The solution is to use out.println() instead, which will send the text to the user's web browser. The asker confirmed this worked for their issue of the file not displaying previously.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
457 views

Read and Display A Local Text File in JSP Page - Stack Overflow

The document discusses displaying a local text file in a JSP page. The original code used System.out.println() to print the file contents, but this sent it to the server log rather than displaying in the browser. The solution is to use out.println() instead, which will send the text to the user's web browser. The asker confirmed this worked for their issue of the file not displaying previously.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

20/09/2016

readanddisplayalocaltextfileinjsppageStackOverflow

signup

login

tour

help

Dismiss

AnnouncingStackOverflowDocumentation
WestartedwithQ&A.Technicaldocumentationisnext,andweneedyourhelp.
Whetheryou'reabeginneroranexperienceddeveloper,youcancontribute.

Signupandstarthelping

LearnmoreaboutDocumentation

readanddisplayalocaltextfileinjsppage
Iwouldliketoreadanddisplayatextfileina
codelikethis:

JSP

page.IfoundnoerrorinmycodebutitdisplayablankafterIrunit.Ihave

<%@pageimport="java.io.FileInputStream"%>
<%@pageimport="java.io.File"%>
<%@pageimport="java.io.InputStreamReader"%>
<%@pageimport="java.net.URL"%>
<%@pageimport="java.io.FileReader"%>
<%@pageimport="java.io.BufferedReader"%>
<%@pagecontentType="text/html"pageEncoding="UTF8"%>
<!DOCTYPEhtml>
<html>
<head>
<metahttpequiv="ContentType"content="text/html;charset=UTF8">
<title>ReadText</title>
</head>
<body>
<%
StringjspPath="C:\\log\\";
StringfileName="log.txt";
StringtxtFilePath=jspPath+fileName;
BufferedReaderreader=newBufferedReader(newFileReader(txtFilePath));
//BufferedReaderbr=newInputStreamReader(newFileInputStream(txtFilePath));
StringBuildersb=newStringBuilder();
Stringline;
while((line=reader.readLine())!=null){
sb.append(line+"\n");
}
System.out.println(sb.toString());
%>
</body>
</html>
file jsp
editedDec20'14at19:20

Farshid
534

askedJul5'14at18:42

raymond_wmt

23

16

Doyouseeanyexceptioninthelogs? SaifAsif Jul5'14at18:49


dear@SaifAsif,isolvedtheproblemalready.Thanks. raymond_wmt Jul5'14at19:07

1Answer

Tosendtextwiththeresponsetouser'swebbrowseruse:
out.println(sb.toString());

because
System.out.println(sb.toString());

willsendittoTomcat'slog,nottotheuser.
answeredJul5'14at18:51

mike_m
1,178

http://stackoverflow.com/questions/24589484/readanddisplayalocaltextfileinjsppage

18

1/2

20/09/2016

readanddisplayalocaltextfileinjsppageStackOverflow

yes.@mike_m...it'sworked,seemlikeiwroteextraonit.Thanksalot. raymond_wmt Jul5'14at19:05

http://stackoverflow.com/questions/24589484/readanddisplayalocaltextfileinjsppage

2/2

You might also like