• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

getting files from WEB-INF

 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 67748
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Sheriff
Posts: 67748
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Evildoers! Eat my justice! And this tiny ad's justice too!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic