• 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

Uploaded File Path

 
Saloon Keeper
Posts: 27762
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

Volodymyr Levytskyi wrote:
Thank you for your help, and one more question:
How to find out root of my deployed web app programmatically except method servletContext.getContextPath and method servletContext.getRealPath?
I need to create new file via file.createNewfile() and I need absolute path to my web app because path like '/myRoot/webapp' doesn't work.



And here I just admonished you to ask new questions under their own separate topics.

I'll split this one off, too.
 
Tim Holloway
Saloon Keeper
Posts: 27762
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Continuing...

DON'T use servletContext.getRealPath to attempt to get a filesystem path to write into. You shoulb never write into or modify your WAR. It may or may not succeed, and even if it does succeed, it almost certainly will come back and bite you someday.

Always upload files to a location that's external to both the webapp (WAR) and to the webapp server.
 
Ranch Hand
Posts: 514
1
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

However, I don't recommend hard-coding this path into the webapp. What I normally do is feed it in as a webapp context variable. If I define the directory path in a Tomcat Context, I can use JNDI to retrieve it.


Can you explain this?
 
Tim Holloway
Saloon Keeper
Posts: 27762
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have this in one of my WEB-INF/web.xml files:


I can override the default path using a Tomcat Context element (other appservers have similar mechanisms)

The application can then retrieve this value using JNDI by looking up "java:comp/env/server/uploadDir".
 
Bin Smith
Ranch Hand
Posts: 514
1
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I use this in my web.xml:

And only this full path works, anything else I tried failed.
In domain.xml I set this for a try:

Actually it didn't override env-entry from my web.xml as file is uploaded well to full path set in web.xml.

Thanks for all your replies!
 
reply
    Bookmark Topic Watch Topic
  • New Topic