aspose file tools*
The moose likes Other Application Frameworks and the fly likes getting files from WEB-INF Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Other Application Frameworks
Bookmark "getting files from WEB-INF" Watch "getting files from WEB-INF" New topic
Author

getting files from WEB-INF

Johnny Gara
Ranch Hand

Joined: Mar 09, 2007
Posts: 43
Is there a way to do this without the ServletContext?

Normally, if I had the servlet context, i would do a ServletContext.getResourceasStream() etc, but I'm using Spring2.5, and I have a class that I would like to have initialized automatically, but can't figure out a way to pass in the ServletContext.

ie,
in my app-context.xml



I need a file to initialize some data in the class.

I've tried doing a

InputStream in = new FileInputStream( "/WEB-INF/my.xml" );

but from my class, i get a null pointer. Any ideas?

thanks
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61202
    
  66

Without a ServletContext you have no way of knowing where the web app has been loaded from, or even if it exists on the file system at all.

I think you need to rethink your design.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Matt Zollinhofer
Ranch Hand

Joined: Jul 09, 2004
Posts: 33
In hopes of continuing the discussion, I'm running into the same problem as Johnny. I'm afraid it is just bad design, but I'm inheriting the code and am having trouble thinking outside of the current design. What is happening is that between environments we want to use different properties, for instance PLACEMARKS=TRUE vs PLACEMARKS=FALSE. So, as part of the build process the appropriate file is placed essentially in the same place Johnny referred to 'WEB-INF/config.xml'. So then the application loads the config.xml into a custom Config class and will appropriately use placemarks or not without having to think about it.

If that explanation makes any sense, any thoughts on what would be the better/best way of doing that?
Johnny Gara
Ranch Hand

Joined: Mar 09, 2007
Posts: 43
Originally posted by Matt Zollinhofer:
In hopes of continuing the discussion, I'm running into the same problem as Johnny. I'm afraid it is just bad design, but I'm inheriting the code and am having trouble thinking outside of the current design. What is happening is that between environments we want to use different properties, for instance PLACEMARKS=TRUE vs PLACEMARKS=FALSE. So, as part of the build process the appropriate file is placed essentially in the same place Johnny referred to 'WEB-INF/config.xml'. So then the application loads the config.xml into a custom Config class and will appropriately use placemarks or not without having to think about it.

If that explanation makes any sense, any thoughts on what would be the better/best way of doing that?


Depends on your environment. Are you running springs 2.5 as well?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61202
    
  66

Perhaps a properties file placed in the classpath rather than in WEB-INF? That way you are freed from the need for a ServletContext.
Johnny Gara
Ranch Hand

Joined: Mar 09, 2007
Posts: 43
Since I'm terrible at keeping track of threads lately lol:


if you are using Spring 2.5, there are a few options you do have. I actually worked around my problem by extending the ApplicationContextAware interface. Of course, this only works if you are injecting your class via spring.

Then it was as simple as using



you can also actually simplify this entire process by simply injecting in the resource as well from your servlet context xml file. This is actually probably the ocrrect way to do things.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: getting files from WEB-INF