When we deploy any web application on server.There is a path something like
http://<host>:<port>/<contextPath>
this to access that deployed webapp.
Now you can set some specific folder in that live web directory where you can save image dynamically.Now with this Get Realpath you can get realpath of your webapp and then append your specified folder's path with that realpath you can save your images so that it will be independent from static path.
Suppose as a example:
I have one application ABC and deployed on http://www.xyz.com/ABC/ (Context path of deployed webapp). Now in database or in property file or in code I specified one folder named as profilepics in war file.In my method I will get realpath(context path) and append that folder name so path is like http://www.xyz.com/ABC/profilepics/ which store images uploaded by user.Donlt forget to add security to that folder if content stored in that directory is confidential.