java - 如何从 servlet 调用在同一 AppEngine 实例上运行的资源 url

标签 java google-app-engine servlets

我已经实现了一些 RESTful 资源 URL,例如 http://myapp.appspot.com/temperature/get与 Jersey 。我还有一个在同一个 AppEngine 实例上运行的 servlet,我想用它来调用资源 url。我的问题是我不知道如何引用 servlet 所在的正在运行的 Appengine 实例。换句话说,我想使用相当于 127.0.0.1 或 $SERVER_HOME 的 AppEngine。

我最初的方法是在 servlet 中执行类似的操作:

URL url = new URL(getServletContext().getContextPath());
HttpURLConnection conn = (HttpURLConnection) url.openConnection();

,但 getContextPath() 返回一个空字符串(它应该是这样,因为我没有将应用程序名称放在 url 中。

如何获取对应用程序基本位置的引用,以便我可以引用在其中运行的 url 资源?提前致谢!

最佳答案

要检查 URL 是否存在于同一服务器计算机中,请执行以下操作:

String host = remoteHost;
int port = remotePort;
String remoteContext = //the application root you want to access;
String remtoePath =  // the servlet or path you want to access;
if((host.equals(request.getServerName()) || host.equals("localhost")) 
      && port == request.getServerPort(){
   ....
  ServletContext context = servletContext.get(remoteContext);
  RequestDispatcher dispatcher = context.getRequestDispatcher(remotePath);
     //if you want to forward
  //dispatcher.forward(request, response);
     //if you want to include
  //dispatcher.include(request, response);

}

关于java - 如何从 servlet 调用在同一 AppEngine 实例上运行的资源 url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10877837/

相关文章:

java - Spring Data MongoDB - 如果 DbRef 字段不存在,则添加到数据库

java - 为什么 java switch 语句不能处理 null,因为它有一个 "default"子句?

python - Google Appengine 和 Python 异常

flash - Google App Engine上的Flash游戏崩溃(其中一些)

java - WAR 文件的规划结构

javascript - Servlet 过滤器不允许在 jsp 文件中上传指向 js 库的链接

java - 在 ViewPager 中选择要开始的 fragment - Android

java - Java返回结果集的方法

java - 如何在 Google App Engine 上重新部署应用程序?

tomcat - IntelliJ 项目在 Tomcat 中返回 404,而在 GlassFish 中运行良好