Question

I am struggling with getting Guice Servlet working to configure how Jetty serves web requests, in this simple case, for static pages.

I have created a simple application that is supposed to map two different requests, one using GuiceServlet, another not. The latter works, while the GuiceServlet mapped one returns a 404 error.

Any tips? I am using: JDK 1.7.0_15; eclipse.jetty.jetty-servlet 8.1.9.v20130131; guice-servlet 3.0. Thanks.

public class Main {
    public static void main(String... args) {
        Guice.createInjector().getInstance(Main.class).start();
    }

    public void start() {
        Server server = new Server(8080);
        ServletContextHandler handler = new ServletContextHandler(server, "/", ServletContextHandler.SESSIONS);
        handler.addEventListener(new MyGuiceServletConfig());
        handler.addServlet(MyServlet.class, "/non-guice");
        server.setHandler(handler);
        try {
            server.start();
            server.join();
        } catch (Exception e) {
            e.printStackTrace();
        }       
    }
}

public class MyGuiceServletConfig extends GuiceServletContextListener {
    @Override
    protected Injector getInjector() {
        return Guice.createInjector(new ServletModule() {
            @Override
            protected void configureServlets() {
                System.out.println("MyGSC->getInjector->configureServlets"); //I'm seeing this in the console...
                serve("/guice").with(MyServlet.class);
            }
        });
    }
}

@Singleton
public class MyServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.getWriter().print("Hello!\nYour path is: " + request.getServletPath());        
    }
}

Beyond this, what's the best way to create the various injectors? I have the main(..) structured like so, so that I can plug in other Modules, leaving the MyServletModule to be specified in MyGuiceServletConfig as I saw somewhere - is this correct?

Was it helpful?

Solution

I ended up being able to this much simpler, in a way that works. Adding a DefaultServlet for the "/" path was necessary:

public class MyMain {
    public static void main(String... args) throws Exception {
        Guice.createInjector(new MyServletModule());
        Server server = new Server(8080);    
        ServletContextHandler handler = 
            new ServletContextHandler(server, "/", ServletContextHandler.SESSIONS);
        handler.addFilter(GuiceFilter.class, "/*", allOf(DispatcherType.class));
        handler.addServlet(DefaultServlet.class, "/");
        server.start();
    }
}

@Singleton
public class MyServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.getWriter().print("Hello!\nYour path is: " + request.getServletPath());        
    }
}

public class MyServletModule extends ServletModule {
    @Override
    protected void configureServlets() {
        serve("/guice").with(MyServlet.class);
    }
}

OTHER TIPS

If you want Jetty to serve the static content, be sure you configure the DefaultServlet too.

Example found in the Jetty embedded examples tree: OneServletContext.java

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.DefaultServlet;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;

public class OneServletContext
{
    public static void main(String[] args) throws Exception
    {
        Server server = new Server(8080);

        ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
        context.setContextPath("/");
        server.setHandler(context);

        // Serve static content from /tmp
        ServletHolder holder = context.addServlet(DefaultServlet.class,"/tmp/*");
        holder.setInitParameter("resourceBase","/tmp");
        holder.setInitParameter("pathInfoOnly","true");

        // Serve some hello world servlets
        context.addServlet(new ServletHolder(new HelloServlet()),"/*");
        context.addServlet(new ServletHolder(new HelloServlet("Buongiorno Mondo")),"/it/*");
        context.addServlet(new ServletHolder(new HelloServlet("Bonjour le Monde")),"/fr/*");

        server.start();
        server.join();
    }
}

This will serve content from your filesystem directory /tmp as the URL context path of http://localhost:8080/tmp/.

Examples:

File System       URL
/tmp/hello.txt    http://localhost:8080/tmp/hello.txt
/tmp/a/hi.txt     http://localhost:8080/tmp/a/hi.txt
/tmp/index.html   http://localhost:8080/tmp/
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top