The ServletContext#getRealPath() method translates 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.
The "/" passed to getRealPath() represents the web content root, the /web folder in the following project structure:
|-- src<br> | :<br> |<br> |-- web<br> | |-- META-INF<br> | | `-- MANIFEST.MF<br> | |-- WEB-INF<br> | | `-- web.xml<br> | |-- index.jsp<br> | `-- login.jsp<br> : <br>
Thus, getRealPath("/") returns the absolute disk file system path of the /web folder of the project's expanded WAR file.
Avoid using getRealPath(), as there are more portable solutions for:
Modifications made to files written into the path returned by getRealPath() are lost upon WAR redeployment.
getRealPath() assumes the WAR file is expanded to the disk file system, which is not always the case. In such scenarios, getRealPath() may return null or an unexpected path.
The above is the detailed content of What does ServletContext.getRealPath('/') return and when should I avoid using it?. For more information, please follow other related articles on the PHP Chinese website!