Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add servlet Filter with embedded jetty

Tags:

I am embedding jetty into my app, and trying to work out how to add servlet filters (for cookie handling). The wiki and the javadoc's dont make it very clear, what am I missing:

Server server = new Server(port); ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS); context.setContextPath("/"); FilterHolder f = new FilterHolder(new AuthorisationFilter()); context.addFilter(... f ...); // ????? context.addServlet(new ServletHolder(new TestServlet()), "/"); 

The only info I have found on this is a forum post suggesting the documentation on this needs to be improved.

like image 332
Jay Avatar asked Jan 18 '13 00:01

Jay


1 Answers

I got the same problem, but I think Καrτhικ's answer is too complex. I found this easy way:

Server server = new Server(8080); ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS); context.setContextPath("/"); context.addServlet(org.eclipse.jetty.servlet.DefaultServlet.class, "/"); context.addFilter(AppFilter.class, "/*", EnumSet.of(DispatcherType.INCLUDE,DispatcherType.REQUEST));  server.setHandler(context); server.start(); server.join(); 

My jetty version is 8.1.14.v20131031.

like image 130
wener Avatar answered Sep 19 '22 12:09

wener