aspose file tools*
The moose likes Struts and the fly likes Reading from and writing to a file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Reading from and writing to a file" Watch "Reading from and writing to a file" New topic
Author

Reading from and writing to a file

Soheil Tayari
Greenhorn

Joined: Apr 04, 2008
Posts: 29
Hi,

I'm fairly new to Struts2. So here's the problem:
In my application I'm using Struts2 as my front-end and I have my own design for the back-end which contains Facades and DAOs. Now from one of my DAOs I'm trying to read a XML file in my web app. I tried to use ServletContextListener to get the context-param but it doesn't work. I don't know how to get a reference to the file. Any help on this issue would be appreciated.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

You'll need to be more specific, and so far I don't see how this is Struts-related.
Soheil Tayari
Greenhorn

Joined: Apr 04, 2008
Posts: 29
Thanks for the reply. Actually the problem is how to get a reference to a file in the web app context and since I was using Struts2 I thought this might be the right place to ask it. And to be more specific, I'm using Tomcat 6 and IntelliJ IDEA as my IDE. Here's the case: For example there's a XML file in folder named "pages" under the root folder. When I run the app when I try to access the file I get a FileNotFoundException. When I trace the exception I see that /pages/file.xml is appended to the Tomcat bin folder and not to the web app context. I also used getRealPath() method in ServletContext but it seems it doesn't work in the .war file. Thank you.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

If it's a war file you're out of luck. You'll need to get the file as a resource from the class path.
Soheil Tayari
Greenhorn

Joined: Apr 04, 2008
Posts: 29
David Newton wrote:If it's a war file you're out of luck. You'll need to get the file as a resource from the class path.


OK. Thank you David.

Here's what I got after searching. I figured that this problem is somehow the one many people have faced. And what are the solutions? If you want to read or write a file or resource in a Plain Java Object in the web application:
1. You have to put your resources in WEB-INF/classes and read them as a stream as David mentioned above.
2. For writing part you got no luck since POJO classes know nothing about the context they're running in. You can somehow pass the context to these classes but that ruins the whole idea of classes being reusable. I think the writing part got something to do with security issues so no one can write any file to your web application.

Of course you always can give absolute paths for reading and writing but I don't think that's a good idea if you want your application to be portable. But If your company got it's own servers I think you can write your file outside your application context. Well that's the only way for writing a file as far as I could find (To give paths outside the context).

Well I appreciate any idea on this.
And to Administrators: I started this thread in Struts forum for the reason I mentioned above. If this is not a right place for this thread please transfer it to the appropriate forum so people with the same problem can participate.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Reading from and writing to a file