Injeniero Barsa
Injeniero Barsa

Reputation: 1175

Base URL for a GWT App

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?

Upvotes: 17

Views: 16914

Answers (4)

Strelok
Strelok

Reputation: 51441

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();
    }
}

Upvotes: 18

Boris Brudnoy
Boris Brudnoy

Reputation: 2460

I suspect this a Tomcat, not a GWT issue. Try renaming your packaged application's WAR file to ROOT.war and placing it in your Tomcat's webapps folder. Tomcat normally treats such an app as one to deploy in the root context (i.e. you'll get your /MyApp.html access).

Upvotes: 0

FGreg
FGreg

Reputation: 15310

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());
}

Upvotes: 2

Dusty Campbell
Dusty Campbell

Reputation: 3156

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.

Upvotes: 6

Related Questions