Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

getting files from WEB-INF

 
Johnny Gara
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • 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
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64629
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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.
 
Matt Zollinhofer
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • 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
  • 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
Author and ninkuma
Marshal
Pie
Posts: 64629
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic