我有一个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);
}
}
现在我需要应用程序中的应用程序root(即/ myApp),我尝试根据Java accessing ServletContext from within restlet Resource得到这个:
Client serverDispatcher = context.getServerDispatcher();
ServletContext servletContext = (ServletContext)serverDispatcher.getContext().getAttributes()
.get("org.restlet.ext.servlet.ServletContext");
String contextPath = servletContext.getContextPath();
在将我的应用程序部署到Tomcat服务器时,这非常正常,但是一旦我使用如上所示的Component启动服务器,我的Context始终为null。有人可以告诉我如何使用restlet内部服务器功能获得正确初始化的上下文吗?
答案 0 :(得分:2)
似乎逻辑。
您需要一个servlet上下文,但是您没有在servlet容器中运行,因此servlet上下文为NULL。
在执行component.start()时,您使用Restlet连接器来处理HTTP / HTTPS请求,而不是像Tomcat这样的servlet容器。
答案 1 :(得分:1)
您需要从Component
类:
component.getDefaultHost().attach("/myApp",
new MyApplication(component.getContext().createChildContext());
这只会给你Restlet上下文,但是Servlet Context仍然不可用,因为这是一个独立的Java应用程序。