File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSP and the fly likes I/O operation in jsp Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "I/O operation in jsp" Watch "I/O operation in jsp" New topic
Author

I/O operation in jsp

Joshua Cloch
Ranch Hand

Joined: Apr 27, 2006
Posts: 95
Hey guys.

With the experience in programming desktop application,it is straightforward to create a file and read that file,etc.The path problem is trivial.

However,in web application,the path may cause problems.I know if I want to read a file I can use

InputStream is = config.getServletContext().getResourceAsStream("/rates.txt");

is this the only way to solve the path problem when reading a file in web application?

what can I do if I want to write file?How can I define the path for the file?

I have tested to pass a servlet-context-relative path like "/rates.txt" to FileWriter directly,it works fine.It writes something in the file in the web directory.But I donot understand how can the FileWriter find the right path to write file?Does the Tomcat server solve these problems automatically?

If I move the FileWriter out of web application.It just write the file in driver C,liek C:\rates.txt.

Thanks very much.


truehh@hotmail.com
Rusty Smythe
Ranch Hand

Joined: Aug 09, 2006
Posts: 93



How can I define the path for the file?
...
But I donot understand how can the FileWriter find the right path to write file?
Does the Tomcat server solve these problems automatically?


I'm not sure what you're asking. If you're confused about paths, you might consult the File javadoc. If you're asking about something else, please elucidate.
[ August 11, 2006: Message edited by: Rusty Smythe ]
Joshua Cloch
Ranch Hand

Joined: Apr 27, 2006
Posts: 95
I am just confused by the path in web application.

say, using FileWriter in servlet with path "/test.txt" will write a file in servlet-context path directory.

if I move the code out of a web application,it will write test.txt in C:\ .
Rusty Smythe
Ranch Hand

Joined: Aug 09, 2006
Posts: 93
Ahh, I see. Okay.

When you write a file to "/test.txt" you are saying "write a file to the top of the local directory."
In windows, that would be C:\
In Linux it is /
and in Tomcat, it is the server's home dir ($CATALINA_HOME, or however your container's home is defined).

Suppose your application is deployed to webapps/example. If you save foo.txt as "/webapps/example/foo.txt" it would go into your app's home folder.

Here's some quick 'n' dirty code that demonstrates this:

[ August 11, 2006: Message edited by: Rusty Smythe ]
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

One thing to keep in mind is that "directories" and "file systems" may not apply in a Java EE webapp. Webapps can be run from an exploded file system. In this case the a call to ServletContext.getRealPath("/") will return the file system path to the root of the web application.

But....
They can also be run from a packed war archive.
When run this way, there are no directories because there is no file system. In this case getRealPath will return null.

If you know your app will always be run from a file system, then you can use getRealPath. If you're not sure, or if you want your app to be portable across containers, you should avoid it.

See:
java.lang.String)" target="_blank" rel="nofollow">http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/ServletContext.html#getRealPath(java.lang.String)


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

Originally posted by Ben Souther:
But....
They can also be run from a packed war archive.
When run this way, there are no directories because there is no file system.
And that implies that you CANNOT write a file into your web application. You can't even update a file that is already there. So if your design involves writing into the web app, you should consider doing something different instead.
Joshua Cloch
Ranch Hand

Joined: Apr 27, 2006
Posts: 95
Thanks very much!

In my website,I have to use a .exe file which must be integrated in website to do some process.

So the only way is to build a file system-supported web application in Tomcat.maybe.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

You can deploy either way (packed war archive or exploded file system) with Tomcat.

Why does the exe file need to be inside the webapp?
Can't it just be in a known location on the machine?
 
 
subject: I/O operation in jsp