gpt4 book ai didi

java - 除了明显的错误配置问题之外,还有什么可能导致 Servlet 过滤器出现 ClassNotFoundException?

转载 作者:搜寻专家 更新时间:2023-11-01 03:50:23 26 4
gpt4 key购买 nike

除了明显的错误配置问题之外,还有什么可能导致 Servlet 过滤器出现 ClassNotFoundException?

我已经在我的 web.xml 文件中定义了 servlet 过滤器:

<filter>
<filter-name>SecurityFilter</filter-name>
<filter-class>com.foo.security.SecurityFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>SecurityFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

而且,我已经在我的类中定义了 servlet 过滤器:

package com.foo.security;
import java.io.IOException;
import java.util.Enumeration;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;


public class SecurityFilter implements Filter{
private static final int MAX_LENGTH = 4096;
@Override
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println("Initializing SecurityFilter!!!");
}

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
try{
System.out.println("Calling SecurityFilter.doFilter()!!!");
chain.doFilter(request, response);
}catch(Exception e){

}finally{

}
}

@Override
public void destroy() {
System.out.println("Destroying SecurityFilter!!!");
}

}

而且我已经确保它包含在 JAR 文件中,并且 JAR 文件包含在 WEB-INF/lib 目录中,并且该类也包含在 WEB-INF/classes 目录中。

但是,我一直收到这个异常:

2015-05-06 13:01:42.926 ERROR   [ScannerThread] org.apache.catalina.core.ContainerBase.[jboss.web].[localhost].[/testApp] - Exception starting filter SecurityFilter

java.lang.ClassNotFoundException: com.foo.security.SecurityFilter
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:249)
at org.apache.catalina.core.ApplicationFilterConfig.setFilterDef(ApplicationFilterConfig.java:397)
at org.apache.catalina.core.ApplicationFilterConfig.<init>(ApplicationFilterConfig.java:108)
at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:3722)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:4367)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:790)
at org.apache.catalina.core.ContainerBase.access$000(ContainerBase.java:122)
at org.apache.catalina.core.ContainerBase$PrivilegedAddChild.run(ContainerBase.java:144)
at java.security.AccessController.doPrivileged(Native Method)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:768)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:553)
at sun.reflect.GeneratedMethodAccessor512.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)

JAR 文件的内容如下:

META-INF/
META-INF/MANIFEST.MF
com/
com/foo/
com/foo/security/
com/foo/security/SecurityFilter.class

WAR 文件的内容如下:

META-INF/
META-INF/MANIFEST.MF
WEB-INF/
WEB-INF/classes/
WEB-INF/classes/com/
WEB-INF/classes/com/foo/
WEB-INF/classes/com/foo/security/
WEB-INF/lib/
WEB-INF/classes/com/foo/security/SecurityFilter.class
WEB-INF/jboss-web.xml
WEB-INF/lib/com.foo.security.jar
WEB-INF/web.xml

最佳答案

这就是问题所在:过滤器类是应用程序启动时最先加载的外部依赖项之一。因此,有多种可能的原因会表现为“未找到筛选器类”,而实际上只是未找到常规类。

这里有一些要检查的东西(希望其他人可以随着时间的推移添加到这个列表中):

关于java - 除了明显的错误配置问题之外,还有什么可能导致 Servlet 过滤器出现 ClassNotFoundException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30133554/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com