I am encountering a problem with setting up the handlers for my web application, what I want is : Having some requests handled by an HTTPServlet with doGet and doPost methods ( how can I load JSP pages from within these methods ? ) and also be able to load static content ( html, JS, CSS )
The way I'm setting it right now, I can only have one or the other, I cannot get both working.
I will explain :
Server server = new Server(5000);
// This is the resource handler for JS & CSS
ResourceHandler resourceHandler = new ResourceHandler();
resourceHandler.setResourceBase(".");
resourceHandler.setDirectoriesListed(false);
// This is the context handler for the HTTPServlet
ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
context.setContextPath("/");
context.addServlet(new ServletHolder(new Main()),"/*");
// this is the Handler list for both handlers
HandlerList handlerList = new HandlerList();
handlerList.setHandlers(new Handler[] { context ,resourceHandler});
/*
If I add them in this order, all requests will be handled by the "context" and no static resource is loaded
If I invert the order, the index page page is loaded by the resource handler, which means, If I activate directory listings, it gives me a list of all directories, otherwise it's just a blank page
I tried working with a WebAppContext to load JSP pages but I still had some problems with which handler should handle which requests
*/
server.setHandler(handlerList);
server.start();
server.join();
Thank you
**EDIT : **
The problem I have is that my HTTP servlet behaves in the following way : Handles all requests leaving nothing for the resource handler, so when a script requests a .js script, it returns an empty html page instead. Here is an example :
WebAppContext root = new WebAppContext();
root.setParentLoaderPriority(true);
root.setContextPath("/");
root.setResourceBase(".");
root.setWelcomeFiles(new String[] {"test.jsp"});
root.addServlet(new ServletHolder(new Main()),"/*");
HandlerList handlerList = new HandlerList();
handlerList.setHandlers(new Handler[] { root });
In this example, when using the root handler without the Main servlet, It loads all static content and jsp pages, but when adding the main servlet, it no longer loads any static content, and responds to all request for static content with an empty html page.