41

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://www.mydomain.com".

Same thing if I deploy it in local tomcat server with 8080 port it should give http://localhost:8080/myapp

Can anyone tell me how to get the root URL of my web application from servlet?

public class MyServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String rootURL="";
        //Code to get the URL where this servlet is deployed

    }
}
blackpanther
  • 10,998
  • 11
  • 48
  • 78
DonX
  • 16,093
  • 21
  • 75
  • 120

5 Answers5

40

You do realize that the URL client sees (and/or types into his browser) and the URL served by the container your servlet is deployed on can be very different?

In order to get the latter, though, you have a few methods available on HttpServletRequest:

  • You can either call getScheme(), getServerName(), getServerPort() and getContextPath() and combine them using appropriate separators
  • OR you can call getRequestURL() and remove getServletPath() and getPathInfo() from it.
ChssPly76
  • 99,456
  • 24
  • 206
  • 195
19

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;
  }
Benny Code
  • 51,456
  • 28
  • 233
  • 198
5

Generally, you can't obtain the URL; but, there are workarounds for specific cases. See Finding your application’s URL with only a ServletContext

Community
  • 1
  • 1
janko
  • 4,123
  • 28
  • 26
4
public static String getBaseUrl(HttpServletRequest request) {
    String scheme = request.getScheme();
    String host = request.getServerName();
    int port = request.getServerPort();
    String contextPath = request.getContextPath();

    String baseUrl = scheme + "://" + host + ((("http".equals(scheme) && port == 80) || ("https".equals(scheme) && port == 443)) ? "" : ":" + port) + contextPath;
    return baseUrl;
}
Mohsen Zamani
  • 486
  • 3
  • 10
3
  1. 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 ); } %>

  2. 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

ThisaruG
  • 3,222
  • 7
  • 38
  • 60
jagan
  • 31
  • 1