Reading request parameters in Google App Engine with Java [duplicate]

℡╲_俬逩灬. 提交于 2019-12-23 19:53:36

问题


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.


回答1:


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")




回答2:


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.




回答3:


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



来源:https://stackoverflow.com/questions/4514940/reading-request-parameters-in-google-app-engine-with-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!