3

There seem to be many questions arround it here but none helped me.... Tried to have single Java Class as startingpoint running embedded Jetty with Jersey to provide either Webpages and JSON interfaces...However even first step failed to provide multiple pages.

this works fine

ServletHolder jerseyServlet = context.addServlet(org.glassfish.jersey.servlet.ServletContainer.class, "/*");
jerseyServlet.setInitOrder(0);      
jerseyServlet.setInitParameter("jersey.config.server.provider.classnames", EntryPoint.class.getCanonicalName());

but adding another content failed. How can I provide multiple pages providing different content types ? Is the only solution to added the content in that single EntryPoint class ?

Thanks in advance for any hint what is needed to change that

public class App {
public static void main(String[] args) throws Exception {
    ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
    context.setContextPath("/");
    Server jettyServer = new Server(8080);
    jettyServer.setHandler(context);

    ServletHolder jerseyServlet = context.addServlet(org.glassfish.jersey.servlet.ServletContainer.class, "/*");
    jerseyServlet.setInitOrder(0);                                                                                  
    jerseyServlet.setInitParameter("jersey.config.server.provider.classnames", EntryPoint.class.getCanonicalName());

    ServletHolder helloWorldServlet = context.addServlet(org.glassfish.jersey.servlet.ServletContainer.class, "/*");
    helloWorldServlet.setInitOrder(1);                                                                                  
    helloWorldServlet.setInitParameter("jersey.config.server.provider.classnames", HelloWorldService.class.getCanonicalName());

    try {
        jettyServer.start();
        jettyServer.join();
    } catch (Exception e){
        System.out.println("Failed running jettyServer with " + e.getMessage());
    } finally {
        jettyServer.destroy();
    }
}

}

user3732793
  • 1,699
  • 4
  • 24
  • 53
  • You create a ServletHolder, but are you adding it to a **ServletContextHandler**, i.e. `context.addServlet(jerseyServlet)`? If you can provide more code people will have a better reference to help you. – Tolio Aug 10 '15 at 17:15
  • tried many. Hopefully the added code of a complete try explains it – user3732793 Aug 11 '15 at 08:08
  • I think I have to give up and do everything through just one entry Point jersey provider – user3732793 Aug 11 '15 at 13:21
  • Use `ServerProperties.PROVIDER_CLASSNAMES` instead of `jersey.config.server.provider.classnames`. – AlikElzin-kilaka Mar 09 '16 at 15:14

3 Answers3

3

Actually found a solution. Missing Key Info was that you simple need for each and everything the right handler, put them in a handler list and voila there you are....

taken from the jetty documentation mostly after finding it

public class JettyServer
{
public static void main(String[] args) throws Exception
{
    // Create a basic Jetty server object that will listen on port 8080.  Note that if you set this to port 0
    // then a randomly available port will be assigned that you can either look in the logs for the port,
    // or programmatically obtain it for use in test cases.
    Server server = new Server(8080);

    // Create the ResourceHandler. It is the object that will actually handle the request for a given file. It is
    // a Jetty Handler object so it is suitable for chaining with other handlers as you will see in other examples.
    ResourceHandler resource_handler = new ResourceHandler();
    // Configure the ResourceHandler. Setting the resource base indicates where the files should be served out of.
    // In this example it is the current directory but it can be configured to anything that the jvm has access to.
    resource_handler.setDirectoriesListed(true);
    resource_handler.setWelcomeFiles(new String[]{ "./html/index.html" });
    resource_handler.setResourceBase(".");

    //Jersey ServletContextHandler
    ServletContextHandler servletContextHandler = new ServletContextHandler(ServletContextHandler.SESSIONS);
    ServletHolder jerseyServlet = servletContextHandler.addServlet(org.glassfish.jersey.servlet.ServletContainer.class, "/api/*");
    jerseyServlet.setInitOrder(0);                                                                                  
    jerseyServlet.setInitParameter("jersey.config.server.provider.classnames", EntryPoint.class.getCanonicalName());

    // Add the ResourceHandler to the server.
    HandlerList handlers = new HandlerList();
    handlers.setHandlers(new Handler[] { resource_handler, servletContextHandler, new DefaultHandler() });
    server.setHandler(handlers);

    // Start things up! By using the server.join() the server thread will join with the current thread.
    // See "http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Thread.html#join()" for more details.
    server.start();
    server.join();
}

}

did help me...

user3732793
  • 1,699
  • 4
  • 24
  • 53
2

I think that is this that you want:

jerseyServlet.setInitParameter("jersey.config.server.provider.classnames",
             String.join(",", Arrays.asList(EntryPoint.class.getCanonicalName(),
                     HelloWorldService.class.getCanonicalName())));
Chambas
  • 21
  • 1
0

You are not adding your ServletHolder instance to the ServletContextHandler.

Also both servlets have the same /* path, I'am not sure but this might not work, try attributing different paths and see if it works.

Do:

context.addServlet(jerseyServlet, "/jersey");

context.addServlet(helloWorldServlet , "/hello");

Community
  • 1
  • 1
Tolio
  • 1,023
  • 13
  • 30
  • thanks, but if I would delete the helloWorldServlet it would work fine as of the inits. You might be right I would have to change it to add multiple Servlets. Still unclear how than a jersey classes does look like. Stillnopt finding any vaild example in the net – user3732793 Aug 11 '15 at 12:55
  • You can use this documentation as reference to [create servlets](https://wiki.eclipse.org/Jetty/Tutorial/Embedding_Jetty#Creating_Servlets) and setting a [Servlet Context](https://wiki.eclipse.org/Jetty/Tutorial/Embedding_Jetty#Servlets_Context). As for what you asked in the title, the problem is that you have two servlets mapped to the same path as I put in my answer. But it seems this is not the only problem. – Tolio Aug 11 '15 at 13:44
  • Thanks, I did but I am also new to Jersey (JAX-RS) where all examples rely on the ServletHolder with the iniProcess. It still is not clear to me how all this interacts. Init is not mentioned in your linked refercences...anyhow as mentioned above I give up and continue with the one entryPoint class – user3732793 Aug 11 '15 at 14:11