Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/311.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/solr/3.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 向guice servlet添加过滤器_Java_Servlets_Guice - Fatal编程技术网

Java 向guice servlet添加过滤器

Java 向guice servlet添加过滤器,java,servlets,guice,Java,Servlets,Guice,我使用guice servet创建了一个servlet,效果很好: protected Injector getInjector() { return Guice.createInjector(new ServletModule() { protected void configureServlets() { bind(MyService.class).to(MyServiceImpl.class); serve("/mys

我使用guice servet创建了一个servlet,效果很好:

protected Injector getInjector() {
    return Guice.createInjector(new ServletModule() {
        protected void configureServlets() {
            bind(MyService.class).to(MyServiceImpl.class);
            serve("/myservlet").with(MyServlet.class);
        }
    });
}
我的servlet如下所示:

@Inject
private MyService myService;

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    response.getWriter().print(myService.hello("John Doe").toString());
}
@Singleton public class MyFilter implements Filter { 
    public void init(FilterConfig filterConfig) throws ServletException { } 
    public void doFilter(ServletRequest request, 
                         ServletResponse response,
                         FilterChain chain) throws IOException, ServletException {
        System.out.println("Yeah"); 
    } 
    public void destroy() { }
}
现在,我尝试向它添加一个过滤器,就像这样:

            bind(MyService.class).to(MyServiceImpl.class);
            filter("/*").through(MyFilter.class);
            serve("/myservlet").with(MyServlet.class);
我的过滤器如下所示:

@Inject
private MyService myService;

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    response.getWriter().print(myService.hello("John Doe").toString());
}
@Singleton public class MyFilter implements Filter { 
    public void init(FilterConfig filterConfig) throws ServletException { } 
    public void doFilter(ServletRequest request, 
                         ServletResponse response,
                         FilterChain chain) throws IOException, ServletException {
        System.out.println("Yeah"); 
    } 
    public void destroy() { }
}
一旦我添加了过滤器,servlet就不再被调用

当我移除过滤器时,servlet再次工作


我做错了什么?

问题不在GUI程序集中,而在过滤器实现中。您必须致电:

chain.doFilter(request, response);

doFilter
方法中,通过过滤器传递处理。您可以在中阅读有关典型筛选器的详细信息。

您的筛选器代码是什么样子的?@Singleton公共类MyFilter实现筛选器{public void init(FilterConfig FilterConfig)抛出ServletException{}public void doFilter(ServletRequest ServletRequest,ServletResponse ServletResponse,FilterChain FilterChain)抛出IOException,ServletException{System.out.println(“耶”);}public void destroy(){}