gautam
gautam

Reputation: 121

getContextPath using servlet

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

Upvotes: 11

Views: 27801

Answers (3)

Ramesh PVK
Ramesh PVK

Reputation: 15446

From the Servlet API there is no way to access the war file name. The war file name is different from the context root. And , even the ServletContext.getRealPath() is the extract location of the war file, which can be different.

Upvotes: 1

BalusC
BalusC

Reputation: 1108577

If the WAR is expanded, you could use ServletContext.getRealPath() in combination with File.getName() to obtain the expanded folder name. This will be the same as the WAR file name.

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

Upvotes: 7

Bozho
Bozho

Reputation: 597016

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

Upvotes: 4

Related Questions