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
getContextPath. String getContextPath() Returns the portion of the request URI that indicates the context of the request. The context path always comes first in a request URI. The path starts with a "/" character but does not end with a "/" character.
1- HttpServletRequest The typical way of getting the context path is through the HttpServletRequest class. Simply you can add a HttpServletRequest parameter to your controller method and then get the context path using getContextPath() method.
The context path of a web application defines the URL that end users will access the application from. A simple context path like myapp means the web app can be accessed from a URL like http://localhost:8080/myapp.
public interface ServletContext. Defines a set of methods that a servlet uses to communicate with its servlet container, for example, to get the MIME type of a file, dispatch requests, or write to a log file. There is one context per "web application" per Java Virtual Machine.
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();
Here is the list of functions that are available, along with a graphic showing how they are related. in this example, the war file name is usually in the "context path" piece, in this case "myapp". This is a folder create by TomCat from the WAR file, and while based on the WAR file name, it does not need to be the same. Look for a folder with a name like "Test".
(From HttpServletRequest Path Decoding)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With