Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

reading a text file from a JSP page

 
suneesh vasudevan
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello
Iam trying to read text file from a JSP page.I kept the text file in the same directory of jsp.But it is not able to locate the file. System
is giving file not found exceptio,
I tried the same code in an application .The its working fine
Can anybody help me
regards
Suneesh
Code which i used in jsp......
<%
com.StringReplace str1=new com.StringReplace();
String str="";
try{
java.io.File file =new java.io.File(register.txt);
StringBuffer message=new StringBuffer();
BufferedReader br=new BufferedReader(new FileReader(file));
while((str=br.readLine())!=null)
{
int i=str.indexOf("<UserName>");
if(i!=-1)
{
str=str1.replaceString(i, 10, str, "suneesh");
}
int j=str.indexOf("<Password>");
if(j!=-1)
{
str=str1.replaceString(j, 10, str, "suneesh");
}
message.append(str+"\n");

}
out.println(message.toString());
}catch(Exception e){System.out.println(e);}
%>
 
Malhar Barai
Author
Ranch Hand
Posts: 399
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi suneesh
Please give the absolute path of the file, as while compiling the jsp file, it won't be able to get the location of the file.
try giving the path.
HTH
MB
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
there is a (semi)related thread here
Basically, the 'default directory' is not what you think it is. You are assuming it is the directory where the JSP is, but it isn't.
The other thread shows how to place a file in the web-inf/classes directory (or somewhere else on the web app classpath - NOT the machine classpath, they are different ) and load it dynamically. It is better not to hard-code file paths since it will make your application non-portable.
Dave
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic