使用内部 RESTlet 服务器的 RESTlet 应用程序上下文为空
我有一个 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 资源中访问 ServletContext:
Client serverDispatcher = context.getServerDispatcher();
ServletContext servletContext = (ServletContext)serverDispatcher.getContext().getAttributes()
.get("org.restlet.ext.servlet.ServletContext");
String contextPath = servletContext.getContextPath();
将我的应用程序部署到Tomcat 服务器,但是当我使用如上所示的组件启动服务器时,我的 Context 始终为空。有人可以告诉我如何使用 Restlet 内部服务器功能获取正确初始化的上下文吗?
I have a Restlet (2.0.10) application, I start with the following code:
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);
}
}
Now I require the applications root (i.e. /myApp) inside the application and I try to get this according to 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();
This works perfectly fine while deploying my application to a Tomcat Server, but as soon as I start the server using a Component as shown above, my Context is always null. Can someone please tell me how to get a properly initialized context using restlets internal server capabilities?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看起来逻辑。
您需要 servlet 上下文,但您没有在 servlet 容器中运行,因此 servlet 上下文为 NULL。
当执行 component.start() 时,您使用 Restlet 连接器来服务器 HTTP/HTTPS 请求,而不是像 Tomcat 这样的 servlet 容器。
Seems logic.
You want a servlet context but you are not running in a servlet container, so the servlet context is NULL.
When doing component.start() you are using the Restlet connectors to server HTTP/HTTPS requests, not a servlet container like Tomcat.
您需要从
Component
类中获取上下文:这只会为您提供 Restlet 上下文,但 Servlet 上下文仍然不可用,因为这是一个独立的 Java 应用程序。
You would need to pick up the context from the
Component
Class:That would just give you the Restlet context, but Servlet Context still won't be available since this is a standalone Java application.