为什么嵌套类不能用作Filter的类

时间:2013-12-05 14:31:36

标签: java servlets filter web.xml nested-class

我正在尝试将嵌套类设置为Filter类,但Intellij IDEA表示,当我尝试在Web容器中运行此应用程序时,它无法找到此类也是如此:

<filter>
    <filter-name>test</filter-name>
    <filter-class>com.web.filters.SimpleFilter.NestedClassFilter</filter-class>
</filter>

我正在尝试使用的课程:

public class SimpleFilter implements Filter {
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;

        doFilter(request, response, filterChain);
    }

    protected void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException{
    }

    public void init(FilterConfig filterConfig) throws ServletException {
    }

    public void destroy() {
    }

    public static class NestedClassFilter extends SimpleFilter {

        @Override
        protected void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
            filterChain.doFilter(request, response);
        }
    }
}

我有例外:

Caused By: java.lang.ClassNotFoundException: com.web.filters.SimpleFilter.NestedClassFilter
        at weblogic.utils.classloaders.GenericClassLoader.findLocalClass(GenericClassLoader.java:297)
        at weblogic.utils.classloaders.GenericClassLoader.findClass(GenericClassLoader.java:270)
        at weblogic.utils.classloaders.ChangeAwareClassLoader.findClass(ChangeAwareClassLoader.java:64)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:247)

1 个答案:

答案 0 :(得分:3)

因为你应该在这里使用完全限定的类名

<filter-class>com.web.filters.SimpleFilter.NestedClassFilter</filter-class>

您放置的不是完全限定的类名。它应该是

<filter-class>com.web.filters.SimpleFilter$NestedClassFilter</filter-class>

请注意$

See here for details why.