将Guice Servlet与Jetty一起使用,可以在不使用web.xml的情况下将路径映射到servlet

时间:2013-03-05 19:19:03

标签: java jetty guice embedded-jetty guice-servlet

我正在努力让Guice Servlet能够配置Jetty如何为静态页面提供Web请求。

我创建了一个简单的应用程序,它应该映射两个不同的请求,一个使用GuiceServlet,另一个不使用。后者工作,而GuiceServlet映射一个返回404错误。

任何提示?我正在使用:JDK 1.7.0_15; eclipse.jetty.jetty-servlet 8.1.9.v20130131; guice-servlet 3.0。感谢。

public class Main {
    public static void main(String... args) {
        Guice.createInjector().getInstance(Main.class).start();
    }

    public void start() {
        Server server = new Server(8080);
        ServletContextHandler handler = new ServletContextHandler(server, "/", ServletContextHandler.SESSIONS);
        handler.addEventListener(new MyGuiceServletConfig());
        handler.addServlet(MyServlet.class, "/non-guice");
        server.setHandler(handler);
        try {
            server.start();
            server.join();
        } catch (Exception e) {
            e.printStackTrace();
        }       
    }
}

public class MyGuiceServletConfig extends GuiceServletContextListener {
    @Override
    protected Injector getInjector() {
        return Guice.createInjector(new ServletModule() {
            @Override
            protected void configureServlets() {
                System.out.println("MyGSC->getInjector->configureServlets"); //I'm seeing this in the console...
                serve("/guice").with(MyServlet.class);
            }
        });
    }
}

@Singleton
public class MyServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.getWriter().print("Hello!\nYour path is: " + request.getServletPath());        
    }
}

除此之外,创建各种注射器的最佳方法是什么?我有这样的main(..)结构,所以我可以插入其他模块,让我在MyGuiceServletConfig中指定MyServletModule,因为我看到了某个地方 - 这是正确的吗?

2 个答案:

答案 0 :(得分:3)

我最终能够以一种有效的方式更简单。必须为“/”路径添加DefaultServlet:

public class MyMain {
    public static void main(String... args) throws Exception {
        Guice.createInjector(new MyServletModule());
        Server server = new Server(8080);    
        ServletContextHandler handler = 
            new ServletContextHandler(server, "/", ServletContextHandler.SESSIONS);
        handler.addFilter(GuiceFilter.class, "/*", allOf(DispatcherType.class));
        handler.addServlet(DefaultServlet.class, "/");
        server.start();
    }
}

@Singleton
public class MyServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.getWriter().print("Hello!\nYour path is: " + request.getServletPath());        
    }
}

public class MyServletModule extends ServletModule {
    @Override
    protected void configureServlets() {
        serve("/guice").with(MyServlet.class);
    }
}

答案 1 :(得分:1)

如果您希望Jetty提供静态内容,请确保也配置DefaultServlet。

Jetty嵌入式示例树中的示例:OneServletContext.java

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.DefaultServlet;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;

public class OneServletContext
{
    public static void main(String[] args) throws Exception
    {
        Server server = new Server(8080);

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

        // Serve static content from /tmp
        ServletHolder holder = context.addServlet(DefaultServlet.class,"/tmp/*");
        holder.setInitParameter("resourceBase","/tmp");
        holder.setInitParameter("pathInfoOnly","true");

        // Serve some hello world servlets
        context.addServlet(new ServletHolder(new HelloServlet()),"/*");
        context.addServlet(new ServletHolder(new HelloServlet("Buongiorno Mondo")),"/it/*");
        context.addServlet(new ServletHolder(new HelloServlet("Bonjour le Monde")),"/fr/*");

        server.start();
        server.join();
    }
}

这会将文件系统目录/tmp中的内容作为http://localhost:8080/tmp/的URL上下文路径提供。

示例:

File System       URL
/tmp/hello.txt    http://localhost:8080/tmp/hello.txt
/tmp/a/hi.txt     http://localhost:8080/tmp/a/hi.txt
/tmp/index.html   http://localhost:8080/tmp/