GetContextPath Using Servlet

The context path in jboss-web.xml is referred to as /Test , but my war file name is Test-0.0.1 . I need the name of this war file using the HttpServlet . Please tell me the name of the function. I tried getContextPath() , but it returns Test. Thanks

+4
source share
3 answers

If the WAR is expanding, you can use ServletContext.getRealPath() in combination with File.getName() to get the name of the extended folder. This will be the same as the WAR file name.

 String name = new File(getServletContext().getRealPath("/")).getName(); 
+6
source

ServletContext.getContextPath() is a way to get the context path. It may differ from the name of the war file, but I can’t think of the reason why you might need the name of the war file.

+2
source

There is no access to the name of the war file from the Servlet API. The name of the war file is different from the root of the context. And even ServletContext.getRealPath() is a place to extract a war file, which may be different.

+1
source

Source: https://habr.com/ru/post/1303680/


All Articles