Skip to content

Instantly share code, notes, and snippets.

@mgenov
Created December 5, 2016 07:13
Show Gist options
  • Save mgenov/969ee05060815b01fc4be6419d4ab825 to your computer and use it in GitHub Desktop.
Save mgenov/969ee05060815b01fc4be6419d4ab825 to your computer and use it in GitHub Desktop.
Jetty Server using Guice
public class Jetty {
private final Server server;
public Jetty(int port) {
this.server = new Server(port);
}
public void start() {
ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
context.setContextPath("/");
context.addServlet(DefaultServlet.class, "/");
context.addFilter(GuiceFilter.class, "/*", EnumSet.of(DispatcherType.REQUEST, DispatcherType.INCLUDE));
context.addEventListener(new GuiceServletContextListener() {
@Override
protected Injector getInjector() {
return Guice.createInjector(new AppModule());
}
});
server.setHandler(context);
try {
server.start();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment