This question already has an answer here:
I need to get the web container path using java, is there any method for that? I need to use it using JSP or Servlet to get a file path.
This question already has an answer here:
I need to get the web container path using java, is there any method for that? I need to use it using JSP or Servlet to get a file path.
You're interested in
getContextPath()
method ofHttpServletRequest
.You're probably looking for
HttpServletRequest#getContextPath()
:...or
getServletPath()
:So the file is stored in the public webcontent of the WAR? Use
ServletContext#getRealPath()
.Note that this only works when the WAR is expanded by the container. Otherwise better use
ServletContext#getResourceAsStream()
if all you really want is to get anInputStream
of it.See also: