我已经实现了一些 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/