Method -1 :
//step1 : import java.net.InetAddress;
InetAddress ip = InetAddress.getLocalHost();
//step2 : provide your file path
String filepath="GIVE YOUR FILE PATH AFTER WEB FOLDER something like /images/grid.png"
//step3 : grab all peices together
String a ="http://"+ip.getHostAddress()+":"+request.getLocalPort()+""+request.getServletContext().getContextPath()+filepath;
Method - 2 :
//Step : 1-get the absolute url
String path = request.getRequestURL().toString();
//Step : 2-then sub string it with the context path
path = path.substring(0, path.indexOf(request.getContextPath()));
//step : 3-provide your file path after web folder
String finalPath = "GIVE YOUR FILE PATH AFTER WEB FOLDER something like /images/grid.png"
path +=finalPath;
MY SUGGESTION
- keep the file which you want to open in the default package of your source folder and open the file directly to make things simple and clear.
NOTE : this happens because it is present in the class path of your IDE if you are coding without IDE then keep it in the place of your java compiled class file or in a common folder which you can access.
HAVE FUN