Reputation: 9464
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
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
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