gpt4 book ai didi

java - 如何修复 ClassNotFoundException : javax. servlet.http.HttpFilter

转载 作者:行者123 更新时间:2023-11-30 10:01:36 26 4
gpt4 key购买 nike

我在启动应用程序后收到 ClassNotFoundException: javax.servlet.http.HttpFilter。

过滤器必须将未登录的用户重定向到登录页面

Pom.xml

<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.3</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.9</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.8</version>
<scope>provided</scope>
</dependency>
</dependencies>

AuthenticationFilter.java

@WebFilter("/to-do/*")
public class AuthenticationFilter extends HttpFilter {
@Override
public void doFilter(HttpServletRequest req, HttpServletResponse res, FilterChain chain) throws IOException, ServletException {
if (AuthenticationService.isAuthenticated(req)){
super.doFilter(req,res,chain);
}else{
res.sendRedirect("/");
}
}
}

Tomcat 日志:

java.lang.NoClassDefFoundError: javax/servlet/http/HttpFilter
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:763)
[...]
Caused by: java.lang.ClassNotFoundException: javax.servlet.http.HttpFilter
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1344)
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1172)
... 59 more

当我发表评论时,@WebFilter 应用程序运行良好。我尝试在 web.xml 中添加 fiter,但它也不起作用;/

最佳答案

您需要至少使用 Tomcat 版本 9 - *HttpFilter** 在 Servlet 4.0 中引入,当时 Tomcat 8 is implementing Servlet 3.1

另一种方法是在 8 版本中添加依赖项 javaee-api

<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>8.0</version>
</dependency>

关于java - 如何修复 ClassNotFoundException : javax. servlet.http.HttpFilter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57346981/

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