Base URL for a GWT App
Solution 1
The doPost
method on RemoteServiceServlet
is passed the HttpServletRequest
sent by the client. HttpServletRequest
has a number of methods available that can construct the base URL of the client's request. HttpServletRequest.getRequestURL() retrieves the whole URL from the request. If you just want the base URL without the page name, you could instead use a combination of getRemoteHost, getRemotePort, and getServletPath.
Example:
doPost(request, response){
String baseURL = new StringBuilder();
baseURL.append(request.getRemoteHost());
baseURL.append(":");
baseURL.append(request.getRemotePort());
baseURL.append(request.getServletPath());
}
Solution 2
Client side:
GWT.getHostPageBaseURL();
or
GWT.getModuleBaseURL();
Server side you can get the URL of any servlet from the HttpServletRequest
by using this simple method:
public static String getBaseUrl( HttpServletRequest request ) {
if ( ( request.getServerPort() == 80 ) ||
( request.getServerPort() == 443 ) ) {
return request.getScheme() + "://" +
request.getServerName() +
request.getContextPath();
} else {
return request.getScheme() + "://" +
request.getServerName() + ":" + request.getServerPort() +
request.getContextPath();
}
}
Solution 3
For client side you can use Window.Location
For example:
public static String getUrlString(String path) {
UrlBuilder urlBuilder = new UrlBuilder();
urlBuilder.setHost(Window.Location.getHost());
urlBuilder.setPath(path);
String port = Window.Location.getPort();
if (!port.isEmpty())
urlBuilder.setPort(Integer.parseInt(port));
return urlBuilder.buildString();
}
Another approach is to use GWT Dictonary. Here you include a snippet of JavaScript in your host HTML page to set the value:
<script type="text/javascript" language="javascript">
var location = { baseUrl: "http://localhost:8080/myapp" };
</script>
Then load the value into the client side with GWT Dictionary:
Dictionary theme = Dictionary.getDictionary("location");
String baseUrl = theme.get("baseUrl");
To use this you would have to change the HTML host page for your local and production instances.
Related videos on Youtube
Injeniero Barsa
Updated on June 16, 2022Comments
-
Injeniero Barsa almost 2 years
I'm developing a GWT App which generates reports for download. When I launch in Eclipse it has an URL like this:
http://127.0.0.1:8888/MyApp.html
But when I package my app for deployment in a web server (i.e. Tomcat) my URL is like this:
http://localhost:8080/MyApp/MyApp.html
Is there any way to get the app's base URL? Like http://localhost:8080/MyApp/ in the second case?
-
Vladimir over 12 yearsWhat do you mean by "get the app's base URL"? Within your servlet or on the client side?
-
-
Injeniero Barsa over 12 yearsThat's an interesting approach. But it works only on the client, right?
-
stephen over 12 yearsYes, using Window.Location only works on the client side. I updated my answer to include another approach as well.
-
Injeniero Barsa over 12 yearsNothing full server side? GWT truly sucks at this.
-
stephen over 12 yearsSorry, all my experience with GWT is client side. Why do you need to know the URL on the server side?
-
Ehsan over 11 yearsHi, thanks for the answer, I get tomcat UnsatisfiedLinkError on either GWT.getModuleBaseUrl() or GWT.getHostPageBaseUrl(). What am I doing wrong? java.lang.UnsatisfiedLinkError: com.google.gwt.core.client.impl.Impl.getModuleBaseURL()Ljava/lang/String;