【发布时间】: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 (“是的”); } 公共无效销毁() { } }