3

I'm modifying the default project that Eclipse creates when you create a new project with Google Web Toolkit and Google App Engine. It is the GreetingService sample project.

How can I read a request parameter in the client's .java file?

For example, the current URL is http://127.0.0.1:8887/MyProj.html?gwt.codesvr=127.0.0.1&foo=bar and I want to use something like request.getParameter("foo") == "bar".

I saw that the documentation mentions the Request class for Python, but I couldn't find the equivalent for Java. It's listed as being in the google.appengine.ext.webapp package, but if I try importing that into my .java file (with a com. prefix), it says that it can't resolve the ext part.

James Moore
  • 8,636
  • 5
  • 71
  • 90
Senseful
  • 86,719
  • 67
  • 308
  • 465

3 Answers3

4

Google App Engine uses the Java Servlet API.

GWT's RemoteServiceServlet provides access to the request through:

HttpServletRequest request = this.getThreadLocalRequest();

from which you can call either request.getQueryString(), and interpret the query string any way you desire, or you can call request.getParameter("foo")

Stephen Denne
  • 36,219
  • 10
  • 45
  • 60
4

I was able to get it to work using Window.Location via this answer:

import com.google.gwt.user.client.Window;

// ...

Window.Location.getParameter("foo") // == "bar"

Note that:

Location is a very simple wrapper, so not all browser quirks are hidden from the user.

Community
  • 1
  • 1
Senseful
  • 86,719
  • 67
  • 308
  • 465
0

Use java.net.URL to parse the URL and then String.split() to parse the query string.

URL url = new URL("http://127.0.0.1:8887/MyProj.html?gwt.codesvr=127.0.0.1&foo=bar");
String query[] = url.getQuery().split("&");
String foo = null;
for (String arg : query) {
  String s[] = arg.split("=");
  if (s[0].equals("foo"))
    System.out.println(s[1]);
}

See http://ideone.com/Da4fY

moinudin
  • 134,091
  • 45
  • 190
  • 216
  • Thanks for the code sample, but the URL should be taken from the current URL the user is viewing... not a static string. – Senseful Dec 23 '10 at 00:40
  • @Senseful You should be able to easily plonk in a string variable. Just remember that if you can't be sure of the validity of the URL, you should do proper exception handling for when say you have the query string `foo&bar` – moinudin Dec 23 '10 at 00:44
  • I'm looking through the documentation to try to find out how to get the current URL... [this question](http://stackoverflow.com/questions/1015442/how-to-get-the-current-server-url-of-appengine-app) didn't help. If you know how, could you please update the answer? – Senseful Dec 23 '10 at 00:48
  • @Senseful Oh, you mean the app engine URL. You should probably ask a separate question for that. – moinudin Dec 23 '10 at 00:49
  • @marcog: alright, I asked it as a [separate question](http://stackoverflow.com/questions/4515052/how-can-i-get-the-current-url-in-google-app-engine-via-client-side-java-code). Thanks for your help. – Senseful Dec 23 '10 at 00:57
  • It doesn't let me use java.net.URL in the client. – Senseful Dec 23 '10 at 01:53
  • @Senseful You can do a `url.split("?")[1]` to get the query string. – moinudin Dec 23 '10 at 11:14