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();
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.
When you work with servlets, there is always a termination at the end of the servlet chain.
It would be either:
DefaultServlet
(when working with a full WebAppContext
)Default404Servlet
(when working with simpler setups like ServletContextHandler
)If all you want the ResourceHandler
for is serving static content, use the DefaultServlet
for your own purposes (its a better choice and supports more functionality as well. such as range requests, caching, auto-gzip, memory-mapped file serving, etc)
Example:
package jetty;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.servlet.DefaultServlet;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
public class ManyDefaultServlet
{
public static void main(String[] args)
{
System.setProperty("org.eclipse.jetty.servlet.LEVEL","DEBUG");
Server server = new Server();
ServerConnector connector = new ServerConnector(server);
connector.setPort(8080);
server.addConnector(connector);
// Setup the basic application "context" for this application at "/"
// This is also known as the handler tree (in jetty speak)
ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
context.setContextPath("/");
server.setHandler(context);
// The filesystem paths we will map
String homePath = System.getProperty("user.home");
String pwdPath = System.getProperty("user.dir");
// Fist, add special pathspec of "/home/" content mapped to the homePath
ServletHolder holderHome = new ServletHolder("static-home", DefaultServlet.class);
holderHome.setInitParameter("resourceBase",homePath);
holderHome.setInitParameter("dirAllowed","true");
holderHome.setInitParameter("pathInfoOnly","true");
context.addServlet(holderHome,"/home/*");
// Lastly, the default servlet for root content
// It is important that this is last.
ServletHolder holderPwd = new ServletHolder("default", DefaultServlet.class);
holderPwd.setInitParameter("resourceBase",pwdPath);
holderPwd.setInitParameter("dirAllowed","true");
context.addServlet(holderPwd,"/");
try
{
server.start();
server.dump(System.err);
server.join();
}
catch (Throwable t)
{
t.printStackTrace(System.err);
}
}
}