Last active
June 29, 2016 23:55
-
-
Save willmenn/0526e9c93588bda671e209ee05d9487d to your computer and use it in GitHub Desktop.
How to bootstrap java application with Jetty java configuration
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 {} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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