In the following snippet:
ServletContext context = request.getServletContext();
String path = context.getRealPath("/");
ServletContext#getRealPath() is intented to convert a web content path (the path in the expanded WAR folder structure on the server's disk file system) to an absolute disk file system path.
"/" represents the web content root. I.e. it represents the
web folder as in the below project structure:
YourWebProject |-- src | : | |-- web | |-- META-INF | | `-- MANIFEST.MF | |-- WEB-INF | | `-- web.xml | |-- index.jsp | `-- login.jsp :
So, passing the
getRealPath() would return you the absolute disk file system path of the
/web folder of the expanded WAR file of the project. Something like
/path/to/server/work/folder/some.war/ which you should be able to further use in
Note that most starters don't seem to see/realize that you can actually pass the whole web content path to it and that they often use
String absolutePathToIndexJSP = servletContext.getRealPath("/") + "index.jsp";
String absolutePathToIndexJSP = servletContext.getRealPath("/index.jsp");
Also note that even though you can write new files into it using
FileOutputStream, all changes (e.g. new files or edited files) will get lost whenever the WAR is redeployed; with the simple reason that all those changes are not contained in the original WAR file. So all starters who are attempting to save uploaded files in there are doing it wrong.
getRealPath() will always return
null or a completely unexpected path when the server isn't configured to expand the WAR file into the disk file system, but instead into e.g. memory as a virtual file system.
getRealPath()is unportable; you'd better never use it
getRealPath() carefully. There are actually no sensible real world use cases for it. If all you actually need is to get an
InputStream of the web resource, better use
ServletContext#getResourceAsStream() instead, this will work regardless of the way how the WAR is expanded. So, if you for example want an
index.jsp, then do not do:
InputStream input = new FileInputStream(servletContext.getRealPath("/index.jsp")); // Wrong!
But instead do:
InputStream input = servletContext.getResourceAsStream("/index.jsp"); // Right!
Or if you intend to obtain a list of all available web resource paths, use
Set<String> resourcePaths = servletContext.getResourcePaths("/");
You can obtain an individual resource as
ServletContext#getResource(). This will return
null when the resource does not exist.
URL resource = servletContext.getResource(path);
Or if you intend to save an uploaded file, or create a temporary file, then see the below "See also" links.