Skip to main content

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

Ok, thanks again for your kind help.  :)

祝好。

束灵杰
Cynric Shu

在 2013年12月6日 星期五,0:13,Joakim Erdfelt 写道:

DefaultServlet is always more performant than ResourceHandler.
The DefaultServlet can do more for performance too (ranged requests, compression, caching, etags, resume, etc ...)

ResourceHandler is an extremely low level and super simple technique for serving content, good for test cases, but it has none of the performance improvement layers that the DefaultServlet provides.

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


On Thu, Dec 5, 2013 at 8:46 AM, cynricshu <cynricshu@xxxxxxxxx> wrote:
Hi, Joakim

Thank you for reply, I have tried your code, and it work well, thank you so much!

But I wonder that, is your solution better than using ResourceHandler (I mean in performance )? 

Best regards,
Cynric Shu

在 2013年12月5日 星期四,22:49,Joakim Erdfelt 写道:

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


_______________________________________________
jetty-users mailing list


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


_______________________________________________
jetty-users mailing list


Back to the top