Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [jetty-users] Handler List problem with Jetty-9.1.0

Don't mix HandlerList with resource handler + default handler with servlet context handlers.

Stay within the ServletContextHandler, and replace the resourceHandler with an alternative DefaultHandler.
Example:

package jetty.examples;

import javax.servlet.MultipartConfigElement;

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 ManyDefaultServletWithMultipart
{
    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");
        String tmpPath = System.getProperty("java.io.tmpdir");
        
        // A Servlet for API Calls
        ServletHolder holderApi = new ServletHolder("api", ExampleApiServlet.class);
        // Configure for MultiPart POST
        holderApi.getRegistration().setMultipartConfig(new MultipartConfigElement(tmpPath));
        context.addServlet(holderApi,"/api/*");

        // 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);
        }
    }
}


--
Joakim Erdfelt <joakim@xxxxxxxxxxx>
Expert advice, services and support from from the Jetty & CometD experts


On Thu, Dec 5, 2013 at 2:48 AM, cynricshu <cynricshu@xxxxxxxxx> wrote:
Hi, 
Recently I have updated Jetty from 9.0.3 to 9.1.0, and after that, I got a problem.
I’m using Jetty in embedded mode, here’s my code:

ServletContextHandler context = new ServletContextHandler(
                ServletContextHandler.SESSIONS);
        context.setContextPath("/");

        ServletHolder holder = new ServletHolder(new jettyHttpRequestHandler());
        holder.getRegistration().setMultipartConfig(
                new MultipartConfigElement(getTmpDir()));

        context.addServlet(holder, "/api/*");

        HandlerList internalHandlers = new HandlerList();
        internalHandlers.addHandler(context);
        internalHandlers.addHandler(resourceHandler);
        internalHandlers.addHandler(defaultHandler);

Server server = new Server();
        server.setHandler(internalHandlers);
server.start();
server.join();

Then I open the start page in browser, the url is http://localhost:8080/views/admin/dashboard.html

In the old version of Jetty(9.0.3), all work well, but with the latest Jetty, I encountered a 404 error. It seems that the Jetty Server didn’t pass the request to resourceHandler. 

I don’t know why, does anyone know?
Thanks.

Best regards,
Cynric Shu


_______________________________________________
jetty-users mailing list
jetty-users@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/jetty-users



Back to the top