• 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

Reading file from a servlet packaged in war

 
Ranch Hand
Posts: 8945
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Is it possible to read a file packaged in a war file from a servlet class that is also packaged in the same war file. If yes how do I specify the path of the file?
Thanx in adv
 
Saloon Keeper
Posts: 27763
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is a variant of a popular question around here.
The problem is that you're assuming that the appserver works with WARs. Actually, a J2EE appserver (and really, Java in general) isn't expected to CARE where its classes and resources came from - if, in fact they were ever even in files to begin with - much less packages in an archive file.
When you invoke a Java classloader (as the appserver is doing), you are actually simply requesting that it return a class/resource from WHEREVER it can find it. And while that CAN be a ".class" file or component of a WAR (or EAR or JAR), it might just as easily have been magically conjured up from a parallel universe. Even though YOU know where it came from, the class loading mechanism isn't allowed to make that assumption.
So the short answer is "no". The ACTUAL answer is, "yes" - IF: 1, you embed it in a class; 2, you place it in a properties file; 3, you resort to some sort of gimmick like retrieving the file in the same way properties files are (by requesting them through the classloader); 4, you place the file in the content area of the WAR (where the JSP's are) and do an internal server request.
All of the above solutions are only good if the file is read-only. For read/write access, you can only reliably locate the file by passing its location as an absoute path to a startup servlet or some similar mechansim that makes that value accessible to the web application.
[ April 23, 2002: Message edited by: Tim Holloway ]
 
Pradeep bhatt
Ranch Hand
Posts: 8945
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
It is possible to read a file by using
getServletContext().getResourceAsStream("/WEB-INF/web.xml"); which will return an InputStream object from which u can read the file.
 
Clowns were never meant to be THAT big! We must destroy it with this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic