This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds and have James Denton on-line!
See this thread for details.
Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Getting path to resources in servlet  RSS feed

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a question of implementation and best practices. I instantiate Class A inside my servlet, which in turn instantiates Class B. The problem is that Class B needs to load xslt stylesheets from WEB-INF/xslt. What is the best way for Class B to know where to load the sheets from? Right now I'm using context.getRealPath("/") within the servlet and then passing the path into Class A and subsequently Class B. Is there a better way to do this? Should I pass the context around instead?
I don't have this problem with my properties files, which are in the classes directory, where I can use getClass().getResourceAsStream("fileName"). I don't think this works outside the classes directory, however. Using Tomcat, btw.
Thanks in advance.
 
Author and ninkuma
Marshal
Posts: 66804
168
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Should I pass the context around instead?


By doing so, you make the classes servlet-environment dependent. If they are already servlet-aware for other reasons, no harm done. But if not, limiting them to the servlet environment just so you can pass the context around would be considered a poor practice.
In such cases, I generally create a File instance indentifying the file/resource and pass that to other classes which I keep as generic as possible.
hth,
bear
 
Wes Hughes
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks.
I think I like the File idea, nice and general.
 
Ranch Hand
Posts: 2676
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could code the actual path to the file as an init-param and use it to instantiate your file. Then if the path changes, you would not need to recompile.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!