Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在Jetty中使用Guice Servlet将路径映射到Servlet,而不使用web.xml_Java_Jetty_Guice_Embedded Jetty_Guice Servlet - Fatal编程技术网

Java 在Jetty中使用Guice Servlet将路径映射到Servlet,而不使用web.xml

Java 在Jetty中使用Guice Servlet将路径映射到Servlet,而不使用web.xml,java,jetty,guice,embedded-jetty,guice-servlet,Java,Jetty,Guice,Embedded Jetty,Guice Servlet,我正在努力让Guice Servlet配置Jetty如何为静态页面提供web请求(在这个简单的例子中) 我创建了一个简单的应用程序,应该映射两个不同的请求,一个使用GuiceServlet,另一个不使用。后者可以工作,而GuiceServlet映射的则返回404错误 有什么建议吗?我使用的是:JDK1.7.015;eclipse.jetty.jetty-servlet 8.1.9.v20130131;GuiceServlet3.0。谢谢 public class Main { publi

我正在努力让Guice Servlet配置Jetty如何为静态页面提供web请求(在这个简单的例子中)

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

有什么建议吗?我使用的是:JDK1.7.015;eclipse.jetty.jetty-servlet 8.1.9.v20130131;GuiceServlet3.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(…)的结构是这样的,这样我就可以插入其他模块,让MyServletModule在MyGuiceServletConfig中指定,正如我在某处看到的那样-这是否正确?

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

Jetty embedded examples树中的示例:

import org.eclipse.jetty.server.server;
导入org.eclipse.jetty.servlet.DefaultServlet;
导入org.eclipse.jetty.servlet.ServletContextHandler;
导入org.eclipse.jetty.servlet.ServletHolder;
公共类OneServletContext
{
公共静态void main(字符串[]args)引发异常
{
服务器=新服务器(8080);
ServletContextHandler上下文=新的ServletContextHandler(ServletContextHandler.SESSIONS);
context.setContextPath(“/”);
setHandler(上下文);
//从/tmp提供静态内容
ServletHolder=context.addServlet(DefaultServlet.class,“/tmp/*”);
holder.setInitParameter(“resourceBase”,即“/tmp”);
holder.setInitParameter(“pathInfoOnly”、“true”);
//提供一些hello world服务
addServlet(新的ServletHolder(新的HelloServlet()),“/*”;
addServlet(新的ServletHolder(新的HelloServlet(“Buongiorno Mondo”)),“/it/*”;
addServlet(新的ServletHolder(新的HelloServlet(“世界你好”),“/fr/*”);
server.start();
join();
}
}
这将把文件系统目录
/tmp
中的内容作为
http://localhost:8080/tmp/

示例:

文件系统URL
/tmp/hello.txthttp://localhost:8080/tmp/hello.txt
/tmp/a/hi.txthttp://localhost:8080/tmp/a/hi.txt
/tmp/index.htmlhttp://localhost:8080/tmp/

我最终能够以一种有效的方式实现这一更简单的功能。需要为“/”路径添加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);
    }
}

但是,它似乎可以正常地为正常添加的servlet(“/non-guice”)提供服务,而不使用它?我可能把问题的标题弄错了。我遇到的问题是,如果我在Guice外部以编程方式指定HelloWorld样式的Servlet,我可以为它们提供服务,但是通过Guice Servlet指定的Servlet不起作用。哦,在这种情况下,请正确设置guiceFilter。。。难道这不能像男人那样通过编程来完成吗?类似于:
FilterHolder-guiceFilter=newfilterHolder(injector.getInstance(guiceFilter.class))
handler.addFilter(guiceFilter,“/*”,EnumSet.allOf(DispatcherType.class))