« »

Coding Quickie – Get URL param string from GWT

Posted January 17th, 2008 by Nazmul

We believe in the power of ONE, at ScreamingToaster. We believe in innovation - ONE experience can change a person's life. We believe in partnerships - ONE endeavor can establish lasting trust & mutual success. We believe in education - ONE training course can empower a developer for life. We believe in giving back - ONE open-source project can inspire future entrepreneur. ONE person can become the leader that starts a revolution. ONE company can change the world and make it a better place - join us.

The Problem

There is no helper class in GWT 1.4.x to access the URL param string that’s passed to the browser. The only way to do this easily would be to use the history mechanism, but that still doesn’t allow you to get key/value pairs, only tokens that are prefixed with "#".

The Solution

With some native JS calls, it’s easy to get the browser’s URL param string. Here’s the code:

public static native String getParamString() /*-{
        return $wnd.location.search;
}-*/;
 
public static HashMap parseParamString(String string){
  String[] ray = string.substring(1, string.length()).split("&");
  HashMap map = new HashMap();
 
  for (int i=0; i<ray.length; i++){
    GWT.log("ray["+i+"]="+ray[i],null);
    String[] substrRay = ray[i].split("=");
    map.put(substrRay[0], substrRay[1]);
  }
 
  return map;
}

Here’s how you use it:

// get the browser's current URL param string (if there is any)
String paramstr = GWTUtils.getParamString();
 
// display the param string to the hosted shell
GWT.log(paramstr, null);
 
// parse the URL param string to a HashMap
HashMap params = GWTUtils.parseParamString(paramstr);
 
// print out the parsed HashMap
GWT.log(params.toString(), null);

This and many other helpful utility functions will be available as part of the developerlife GWT module that we will release very soon.

 

We believe in the power of ONE, at ScreamingToaster. We believe in innovation - ONE experience can change a person's life. We believe in partnerships - ONE endeavor can establish lasting trust & mutual success. We believe in education - ONE training course can empower a developer for life. We believe in giving back - ONE open-source project can inspire future entrepreneur. ONE person can become the leader that starts a revolution. ONE company can change the world and make it a better place - join us.

2 Responses to “Coding Quickie – Get URL param string from GWT”

  1. am Says:

    try this instead:

    Window.Location.getParameter(“param”)

  2. Xepra Says:

    Ditto am… I wish you would update your entry with this up top.

    I didn’t notice am’s comment until I came back here to post the same thing ;p

Leave a Reply