2

I deployed my webapp in Tomcat, but when I prefix the filename with \ I end up in the root of my server (C) folder, and if I don't prefix it with \ I end up in the bin folder. The former I expected, the latter I didn't. What's the cleanest way to write to a folder in my webapp(userControlWebApp\images) because just doing ..\webapps\ + request.getContextPath() doesn't seem the cleanest way.

2
  • Can't you use an absolute path? Commented Jan 6, 2011 at 12:34
  • That's well so absolute... I like to keep it a bit dynamic. Commented Jan 6, 2011 at 12:36

1 Answer 1

8

Use getServletContext().getRealPath(..)

Sign up to request clarification or add additional context in comments.

3 Comments

Can't believe I forgot that one. Just tried System.getProperty("user.dir") and that also seems to work. Any reason to use one over the other?
prefer the API - getRealPath. I'm not sure if the other is guaranteed to work always.
Note that the folder and all its contents will get lost whenever you redeploy the webapp. It's not a good choice if you want to make it permanent. See also this related question stackoverflow.com/questions/4548190/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.