我有一个 Restlet (2.0.10) 应用程序,我从以下代码开始:
public static void main(final String[] args) {
try {
// Create a new Component
final Component component = new Component();
// Add a new HTTP server listening on port 8182
component.getServers().add(Protocol.HTTP, SERVER_PORT);
// Add a client protocol connector for static files
component.getClients().add(Protocol.FILE);
// Attach the sample application.
component.getDefaultHost().attach("/myApp", new MyApplication(Context.getCurrent()));
// Start the component.
component.start();
} catch (Exception e) {
LOGGER.error(e);
}
}
现在我需要应用程序内的应用程序根(即 /myApp),我尝试根据Java 从 restlet Resource 访问 ServletContext来获取它:
Client serverDispatcher = context.getServerDispatcher();
ServletContext servletContext = (ServletContext)serverDispatcher.getContext().getAttributes()
.get("org.restlet.ext.servlet.ServletContext");
String contextPath = servletContext.getContextPath();
这在将我的应用程序部署到 Tomcat 服务器时工作得非常好,但是一旦我使用如上所示的组件启动服务器,我的 Context 始终为空。有人可以告诉我如何使用restlets内部服务器功能获得正确初始化的上下文吗?