Only 44 hours left in the trailboss' kickstarter!

New rewards and stretch goals. CLICK HERE!



Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Reading File from outside the WEBROOT  RSS feed

 
sk mehrotra
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am trying to open a text file from jsp page.
when the file is in the web root, then the file is open, but when i copy the file to some other directory like D drive, then its showing error: "The requested URL /servlets/oracle.jsp.JspServlet was not found on this server". This error shows that its not finding the file.

The Code i am using :
<%@page contentType="text/html" import="java.io.*" %>
<%
String path="E://REPORT//test.txt";
out.println(path);
response.setContentType("text");
response.setHeader("content-disposition","inline;filename="+path);
String temp=request.getRealPath(path);
//out.println(temp);
FileInputStream fin;
try
{
fin = new FileInputStream (temp);
out.println( new DataInputStream(fin).readLine() );
fin.close();
}
catch (IOException e)
{
out.println ("Unable to read from file" +e);
System.exit(-1);
}
%>

Thanks for your Help

Regards
[ May 10, 2007: Message edited by: sk mehrotra ]
 
Sonny Gill
Ranch Hand
Posts: 1211
IntelliJ IDE Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by sk mehrotra:

String path="E://REPORT//test.txt";
...
catch (IOException e)
{
out.println ("Unable to read from file" +e);
System.exit(-1);
}
[ May 10, 2007: Message edited by: sk mehrotra ]


There are quite a few things that are sort of wrong in that code. Please study some resources on how to handle exceptions, servlet/jsp environment, MVC architecture etc..

Anyhow -
Is the second slash after REPORT ('//') a mistake?
Do not use System.exit() in a JSP running in a servlet container!!!
At the very least, wrap the IOException in a RuntimeException and throw it again. At least, that will show you why it is failing.
 
Hussam Bamatraf
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

You should not use request.getRealPath() with absolute path (E://REPORT//test.txt) because it is already real full path.
You use request.getRealPath only if you have file relative to your web application folder. By the way, this method is deprecates and you should use ServletContext.getRealPath() or application.getRealPath() from your JSP.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!