You are on page 1of 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