Forums Register Login

I/O operation in jsp

+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send



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 ]
+Pie Number of slices to send: Send
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:\ .
+Pie Number of slices to send: Send
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 ]
+Pie Number of slices to send: Send
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:
http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/ServletContext.html#getRealPath(java.lang.String)
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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?
If tomatoes are a fruit, then ketchup must be a jam. Taste this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1662 times.
Similar Threads
Problem with file and writing UTF-8 information into file
Path used for file creation in a WSAD Web App
Writing a log file for my servlet
Problem with file and writing UTF-8 information into file
to find the workspace path at runtime to write the file
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 22:40:40.