3

My Java web application contains a startup servlet. Its init() method is invoked, when the web application server (Tomcat) is started. Within this method I need the URL of my web application. Since there is no HttpServletRequest, how to get this information?

2
  • 3
    You can't. What do you need it for? Please elaborate the functional requirement instead of asking how to achieve a solution of which you thought that it is the right solution but which may not be the right solution after all. Maybe that the requirement in turn can reasonably be answered. Commented Jun 29, 2010 at 11:51
  • I want to send an email which contains the URL to the web application. At least, the context path would be nice. Commented Jun 29, 2010 at 13:59

4 Answers 4

5

You can't. Because there is no "URL of an Java web application" as seen "from within". A servlet is not tied to an URL, that is done from the outside. (Perhaps you have a Apache server that connects to a Tomcat - Tomcat can't know about it) It makes sense to ask a HttpServletRequest for its url, because we are speaking of the information of a event (the URL that was actually used to generate this request), it does not make sense to ask for a configuration URL.

Sign up to request clarification or add additional context in comments.

Comments

1

A workaround could be to perform the initialization lazy when the first request arrives. You can implement a filter that do that once, e.g. by storing a boolean flag in a static variable and synchronizing access to the flag correctly. But it implies a little overhead because each subsequent request will go through the filter which then bypass the initialization. It was just a thought.

Comments

1

There is nothing in the servlet API that provides this information, plus any given resource may be bound to multiple URL's.

What you CAN do, is to inspect the servlet context when you receive an actual request and see what URL was used.

Comments

1

Here is how it works for me and probably for most configurations:

public static String getWebappUrl(ServletConfig servletConfig, boolean ssl) {
    String protocol = ssl ? "https" : "http";
    String host = getHostName();
    String context = servletConfig.getServletContext().getServletContextName();
    return protocol + "://" + host + "/" + context;
}

public static String getHostName() {
    String[] hostnames = getHostNames();
    if (hostnames.length == 0) return "localhost";
    if (hostnames.length == 1) return hostnames[0];
    for (int i = 0; i < hostnames.length; i++) {
        if (!"localhost".equals(hostnames[i])) return hostnames[i];
    }
    return hostnames[0];
}

public static String[] getHostNames() {
    String localhostName;
    try {
        localhostName = InetAddress.getLocalHost().getHostName();
    } catch (UnknownHostException ex) {
        return new String[] {"localhost"};
    }
    InetAddress ia[];
    try {
        ia = InetAddress.getAllByName(localhostName);
    } catch (UnknownHostException ex) {
        return new String[] {localhostName};
    }
    String[] sa = new String[ia.length];
    for (int i = 0; i < ia.length; i++) {
        sa[i] = ia[i].getHostName();
    }
    return sa;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.