I want to find context path of my web Application in normal Java class.If I find I can specify paths like this /Rod1/thermalMap.exe
wherever I need.
I know, How to find in servlet
using the following code
getServletContext().getRealPath("");
My webApps folder in the following way.
You can get the absolute path to to your webApp/WEB-INF/classes
directory as below:
URL resource = getClass().getResource("/");
String path = resource.getPath();
This will return you an absolute path like this:
/C:/SERVERS/x/y/x/yourApp/WEB-INF/classes
And from this you can get the path to the yourApp
directory:
path = path.replace("WEB-INF/classes/", "");
which you can use to specify paths like /Rod1/thermalMap.exe
, by appending to this path
.
你试试这个吗?
String path = new File(".").getCanonicalPath();
You need to register a
javax.servlet.ServletContextListener
in your web.xml like this:
<listener>
<listener-class>com.my.Servlet</listener-class>
</listener>
From that, you can get the ServletContext on which you can call getContextPath().
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.