RESTlet application context is null using internal RESTlet Server

I have a Restlet application (2.0.10), 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 need a root application (i.e. / myApp) inside the application, and I'm trying to get it according to Java by accessing the ServletContext from the Restlet resource :

 Client serverDispatcher = context.getServerDispatcher(); ServletContext servletContext = (ServletContext)serverDispatcher.getContext().getAttributes() .get("org.restlet.ext.servlet.ServletContext"); String contextPath = servletContext.getContextPath(); 

This works great when deploying my application to Tomcat Server, but as soon as I start the server using the component as shown above, my context is always zero. Can someone please tell me how to get a correctly initialized context using the capabilities of the internal server of restals?

+4
source share
2 answers

It seems like logic.

You need a servlet context, but you are not working in a servlet container, so the servlet context is NULL.

When using component.start (), you use Restlet connectors for HTTP / HTTPS server requests, and not for a servlet container, such as Tomcat.

+2
source

You will need to select a context from the Component class:

 component.getDefaultHost().attach("/myApp", new MyApplication(component.getContext().createChildContext()); 

This will just give you a Restlet context, but the servlet context will still not be available as it is a standalone Java application.

+1
source

Source: https://habr.com/ru/post/1382665/


All Articles