This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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.
An IDE is no substitute for an Intelligent Developer.
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.
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.