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:/
This function helps you to get your base URL from a HttpServletRequest
:
public static String getBaseUrl(HttpServletRequest request) {
String scheme = request.getScheme() + "://";
String serverName = request.getServerName();
String serverPort = (request.getServerPort() == 80) ? "" : ":" + request.getServerPort();
String contextPath = request.getContextPath();
return scheme + serverName + serverPort + contextPath;
}