0

我在作为参考添加的一个 jar 文件中有一个过滤器。

我的项目是春天性质的。我开发了一个网络服务。对这个 Web 服务的所有请求都应该被我的过滤器“HelloWorld”拦截。

此过滤器位于其中一个参考文件中。

在这里,我想我会将它实现为拦截器。

参考文件中的过滤器看起来像

public class HelloWorld implements Filter {
private static final Logger _logger = Logger.getLogger(HelloWorld.class.getName());

  protected String name = null;

  public HelloWorld()
  {
  }

  public HelloWorld(String name) {
    this.name = name;
  }

    public void doFilter(ServletRequest req, ServletResponse res,
            FilterChain chain) throws IOException, ServletException {

        HttpServletRequest request = (HttpServletRequest) req;

        PrintWriter out = response.getWriter();
        out.println("Hello "+name);     
        chain.doFilter(req, res);
    }
    public void init(FilterConfig config) throws ServletException {

        //Get init parameter
        String testParam = config.getInitParameter("test");

        //Print the init parameter
        System.out.println("test param: " + testParam);
    }
    public void destroy() {
        //add code to release any resource
    }

    //some other methods as well 
}

实现这一点的最佳方法是什么。由于应用程序的限制,我无法在 web.xml 中配置过滤器。

我们是否可以直接将这个 HelloWorld 过滤器作为拦截器的引用,使其行为类似于拦截器。我们可以在spring中将此过滤器更改为拦截器并在spring.xml中进行配置而不更改功能。

抱歉,如果我的问题很简单。

谢谢。

4

0 回答 0