How should I configure for this ?
Depends on the server used. This is not controllable from inside the WAR. Just consult the server-specific documentation how to configure the context path.
Note that this thus means that you should never hardcode the context path in your webapp's code and in Java source code always rely on getContextPath()
method of HttpServletRequest
and/or ServletContext
and in Facelets source code on <h:outputStylesheet>
, <h:outputScript>
, <h:graphicImage>
, <h:link>
, <h:button>
, etc components which automatically prepend the context path, or #{request.contextPath}
to print it in plain <a>
, <link>
, etc elements.
I did found a way to accomplish this by specifying in context.xml(within the project directory) the path attribute as path="/". But not sure this is the best way to do it ?!
That's indeed the right way specifically for Tomcat.
An alternative would be to rename the WAR file to ROOT.war
(and delete/rename the old apache-tomcat/webapps/ROOT
folder representing the Tomcat default homepage). But this is generally somewhat clumsy. Generally you'd like to have a more specific filename of the WAR, often including the build version and leave the job to the context.xml
.
Another alternative is to front the Tomcat instance by a HTTPD instance. This is also what most production servers do. This has among others the additional advantage that you can configure HTTPD to show a special "Sorry, this website is in maintenance, come back later!" page as HTTP 503 error page which would show up when you shutdown/restart/redeploy Tomcat.
Another concern is that how would I then access the Tomcat server's root page, which I could previously access at localhost:8080?
Simply rename apache-tomcat/webapps/ROOT
folder to the desired name which should represent the new context path. E.g. apache-tomcat/webapps/tomcathome
which should then make it available on http://localhost:8080/tomcathome
.