Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
172 views
in Technique[技术] by (71.8m points)

java - Serving static files from alternate path in embedded Jetty

I'm trying to create an embedded jetty server with both a custom servlet that serves some dynamic data, and default servlet that will serve some images. I have the custom servlet working, but I can't figure out how to add a default servlet to serve the image files.

This is what I have...

private void setUpServer(ServerOptions options){
    s = new Server(options.getPort());
    this.options = options;
    context = new ServletContextHandler(ServletContextHandler.SESSIONS);
    context.setContextPath("/");
    s.setHandler(context);
    context.addServlet(new ServletHolder(new DataServlet()), "/data/*");
    context.addServlet(new ServletHolder(new DefaultServlet()), "/pictures/*");
}

I can't figure out how to configure the DefaultServlet to work as a file server and still have the custom DataServelet still work.

Does anyone have any ideas?

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

What you need:

  1. A DefaultServlet at "/" (recommended, it is a requirement of the servlet spec)
    • this should be at named dispatcher of "default" (another requirement of servlet spec)
  2. An alternate DefaultServlet with your custom static content, configured via init-params
    • Using a different named dispatcher than "default" (to avoid a name collision between other servlet spec features)
    • You can use the ServletHolder.setInitParameter(name,value) to accomplish this
    • Be sure you set the pathInfoOnly parameter to true (to get around special cases for "default" named dispatchers)
  3. A servlet of your own, serving dynamic content.

AltDefaultServlet.java

package jetty.demo;

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 AltDefaultServlet
{
    public static void main(String[] args)
    {
        System.setProperty("org.eclipse.jetty.LEVEL","INFO");

        Server server = new Server();
        ServerConnector connector = new ServerConnector(server);
        connector.setPort(8080);
        server.addConnector(connector);

        // The filesystem paths we will map
        String homePath = System.getProperty("user.home");
        String pwdPath = System.getProperty("user.dir");

        // 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.setBaseResource(Resource.newResource(pwdPath));
        context.setContextPath("/");
        server.setHandler(context);

        // add a simple Servlet at "/dynamic/*"
        ServletHolder holderDynamic = new ServletHolder("dynamic", DynamicServlet.class);
        context.addServlet(holderDynamic, "/dynamic/*");

        // 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 (always needed, to satisfy servlet spec)
        // It is important that this is last.
        ServletHolder holderPwd = new ServletHolder("default", DefaultServlet.class);
        holderPwd.setInitParameter("dirAllowed","true");
        context.addServlet(holderPwd,"/");

        try
        {
            server.start();
            server.dump(System.err);
            server.join();
        }
        catch (Throwable t)
        {
            t.printStackTrace(System.err);
        }
    }

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...