I'm modifying the default project that Eclipse creates when you create a new project with Google Web Toolkit and Google App Eng开发者_Python百科ine. 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.
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")
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.
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
精彩评论