• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

I/O operation in jsp

 
Joshua Cloch
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rusty Smythe
Ranch Hand
Posts: 93
Mac Objective C Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



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
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 93
Mac Objective C Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)
 
Paul Clapham
Sheriff
Pie
Posts: 20719
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic