Skip to content

Instantly share code, notes, and snippets.

@willmenn
Last active June 29, 2016 23:55
Show Gist options
  • Save willmenn/0526e9c93588bda671e209ee05d9487d to your computer and use it in GitHub Desktop.
Save willmenn/0526e9c93588bda671e209ee05d9487d to your computer and use it in GitHub Desktop.
How to bootstrap java application with Jetty java configuration
public class BootstrapApp {
public static void main(String[] args) throws Exception {
ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
addContextPath(context);
addServlets(context);
addFilters(context);
buildAndStartServer(context);
}
private static void buildAndStartServer(ServletContextHandler context) throws Exception {
Server server = new Server(8080);
server.setHandler(context0);
server.start();
server.join();
}
private static void addContextPath(ServletContextHandler context) {
context0.setContextPath("/ExampleApp");
}
private static void addServlets(ServletContextHandler context) {
context0.addServlet(new ServletHolder(new HelloController()), "/hello.html");
context0.addServlet(new ServletHolder(new ExampleController()), "/example.html");
}
private static void addFilters(ServletContextHandler context) {
context0.addFilter(FilterExample.class, "/hello.html", EnumSet.of(DispatcherType.REQUEST));
context0.addFilter(FilterExample.class, "/example.html", EnumSet.of(DispatcherType.REQUEST));
}
}
public class FilterExample implements Filter {
public void destroy() {}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {}
public void init(FilterConfig config) throws ServletException {}
}
public class HelloController extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment