【问题标题】:Adding filter to guice servlet将过滤器添加到 guice servlet
【发布时间】:2016-06-10 11:07:58
【问题描述】:

我使用 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());
}

现在我正在尝试向它添加一个过滤器:

            bind(MyService.class).to(MyServiceImpl.class);
            filter("/*").through(MyFilter.class);
            serve("/myservlet").with(MyServlet.class);

我的过滤器如下所示:

@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 再次工作。

我做错了什么?

【问题讨论】:

  • 你的过滤代码是什么样子的?
  • @Singleton public class MyFilter implements Filter { public void init(FilterConfig filterConfig) throws ServletException { } public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { System.out.println (“是的”); } 公共无效销毁() { } }

标签: java servlets guice


【解决方案1】:

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

chain.doFilter(request, response);

在您的doFilter 方法中通过过滤器传递处理。您可以在javadoc 中阅读有关典型过滤器的更多信息。

【讨论】:

    猜你喜欢
    • 2012-02-19
    • 2015-11-19
    • 1970-01-01
    • 2012-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-21
    • 2016-12-08
    相关资源
    最近更新 更多