Read and Display A Local Text File in JSP Page - Stack Overflow
Read and Display A Local Text File in JSP Page - Stack Overflow
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
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
http://stackoverflow.com/questions/24589484/readanddisplayalocaltextfileinjsppage
2/2