2016-06-10 29 views
2

我使用吉斯SERVET的正常工作創建一個servlet:添加過濾器吉斯的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再次工作。

我在做什麼錯?

+0

你的過濾代碼是怎麼樣的? –

+0

@Singleton 公共類MyFilter實現過濾{ 公共無效的init(一個FilterConfig一個FilterConfig)拋出的ServletException { } 公共無效的doFilter(ServletRequest中的servletRequest,ServletResponse的ServletResponse的,FilterChain filterChain)拋出IOException異常,的ServletException { System.out的。的println( 「是」); } public void destroy(){ } } – javapenguin

回答

1

問題不在於你的Guice程序集,而在於你的過濾器實現。你將不得不撥打:

chain.doFilter(request, response); 

doFilter方法通過過濾器上的處理來傳遞。您可以閱讀有關javadoc中的典型過濾器的更多信息。