csvan
csvan

Reputation: 9464

Java EE: how to get the URL of my application?

In Java EE, how can I dynamically retrieve the full URL for my application?

For example, if the URL is "localhost:8080/myapplication/", I would like a method that can simply return this to me, either as a String or something else.

I am running GlassFish as application server.

Upvotes: 4

Views: 7144

Answers (2)

Marco
Marco

Reputation: 73

In JSF I was able to achieve this relatively easily using the following helper function (all you need is a reference to the HttpServletRequest:

public static String getRequestUrl() {
    HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
    StringBuffer requestURL = request.getRequestURL();
    if (request.getQueryString() != null) {
        requestURL.append("?");
        requestURL.append(request.getQueryString());
    }
    return requestURL.toString();
}

Upvotes: 0

Tomasz Nurkiewicz
Tomasz Nurkiewicz

Reputation: 340963

Inside any servlet or filter you have access to HttpServletRequest which allows you to see what URL was used by the client to access your application, e.g. try HttpServletRequest.getRequestURL().

Upvotes: 7

Related Questions