How to make Jetty dynamically load “static” pages

前端 未结 10 800
耶瑟儿~
耶瑟儿~ 2020-12-02 14:57

I am building Java web applications, and I hate the traditional \"code-compile-deploy-test\" cycle. I want to type in one tiny change, then see the result INSTANTLY, without

10条回答
  •  醉酒成梦
    2020-12-02 15:15

    While one of the answers above is exactly right for configuring jetty by xml, if you want to configure this option in code (for an embedded server) the answer is different and not found on that page.

    You'll find a number of suggestions online including

    context.getInitParams().put("useFileMappedBuffer", "false");

    Or overriding the WebAppContext, or using a fully qualified name for the init parameter. None of these suggestions worked for me (using Jetty 7.2.2). Part of the problem was that the useFileMappedBuffer option needs to be set on the servlet that the WebAppContext is using to serve the static files, rather than on the context.

    In the end I did something like this on a straightforward ServletContextHandler

    // Startup stuff
    final Server server = new Server(port);
    ServletContextHandler handler = new ServletContextHandler();
    handler.setResourceBase(path);
    
    SessionManager sm = new HashSessionManager();
    SessionHandler sh = new SessionHandler(sm);
    handler.setSessionHandler(sh);
    
    DefaultServlet defaultServlet = new DefaultServlet();
    ServletHolder holder = new ServletHolder(defaultServlet);
    holder.setInitParameter("useFileMappedBuffer", "false");
    handler.addServlet(holder, "/");
    
    server.setHandler(handler);
    server.start();
    server.join();
    

提交回复
热议问题