I want to get the root url of my web application from one of the servlet.
If I deploy my application in \"www.mydomain.com\" I want to get the root url like \"http:/
Write a scriptlet in the welcome file to capture the root path. I assume index.jsp is the default file. So put the following code in that
<%
RootContextUtil rootCtx = RootContextUtil.getInstance();
if( rootCtx.getRootURL()==null ){
String url = request.getRequestURL().toString();
String uri = request.getRequestURI();
String root = url.substring( 0, url.indexOf(uri) );
rootCtx.setRootURL( root );
}
%>
Use this variable wherever needed within the application directly by calling the value as
String rootUrl = RootContextUtil.getInstance().getRootURL();
NOTE: No need to worry about protocols/ports/etc.. Hope this helps every one