tags:

views:

1358

answers:

1

I have a Google Web Toolkit (GWT) application and when I link to it, I want to pass some arguments/parameters that it can use to dynamically retrieve data. E.g. if it were a stock chart application, I would want my link to contain the symbol and then have the GWT app read that and make a request to some stock service. E.g. http://myapp/gwt/StockChart?symbol=GOOG would be the link to my StockChart GWT app and it would make a request to my stock info web service for the GOOG stock.

So far, I've been using the server-side code to add Javascript variables to the page and then I've read those variables using JSNI (JavaScript Native Interface).

For example:

In the host HTML:

<script type="text/javascript">
  var stockSymbol = '<%= request.getParameter("symbol") %>';
</script>

In the GWT code:

public static native String getSymbol() /*-{
    return $wnd.stockSymbol;
}-*/;

(Although this code is based on real code that works, I've modified it for this question so I might have goofed somewhere)

However, this doesn't always work well in hosted mode (especially with arrays) and since JSNI wasn't around in version 1.4 and previous, I'm guessing there's another/better way.

+6  A: 

If you want to read query string parameters from the request you can use the com.google.gwt.user.client.Window class:

// returns whole query string 
public static String getQueryString() 
{
    return Window.Location.getQueryString();
}

// returns specific parameter
public static String getQueryString(String name)
{   
    return Window.Location.getParameter(name);
}
Drejc
That works for GET requests. What about POST parameters? For example, if I wanted to request 200 stock symbols at once I wouldn't want them all in the URL
KC Baltz
I would suggest to create a widget which acts according to some parameter. You certainly don't want to build your page with POST/GET, build it upon an XML send to the page (RPC) and parsed on the client to create a all widgets. Widgets then individualy call the server to get the data displayed.
Drejc
PS: You must get used to the GWT (RPC) asynchrounous (AJAX) way of doing things. GET and POST should only be used to influence some global behaviour (for instance language selection) as it reloads the whole page.
Drejc
It's really a boot-strapping issue. Once the app is loaded, then I'm using either GWT RPC or HTTP requests if that's not available. This question was motivated by my current project which is an app to display items based on user selection. With a large choice of items, POST is a natural fit.
KC Baltz