[英]Maven cannot be cast to javax.servlet.Filter
I'm beginning to develope a project using maven but when I launch it it say that my Filter cannot be cast to javax.servle.Filter. 我开始使用Maven开发项目,但是当我启动它时,它说我的过滤器无法转换为javax.servle.Filter。
This is the exception: 这是例外:
GRAVE: Exception starting filter FilterLogin
java.lang.ClassCastException: it.tgi.cd.servlet.FilterLogin cannot be cast to javax.servlet.Filter
at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:260)
at org.apache.catalina.core.ApplicationFilterConfig.<init>(ApplicationFilterConfig.java:107)
at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:4775)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5452)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
and this is my pom.xml 这是我的pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>it.project</groupId>
<artifactId>Projet</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.2.2.Final</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.0-b01</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.21</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-catalina</artifactId>
<version>8.0.32</version>
</dependency>
</dependencies>
<build>
<finalName>SimpleProject</finalName>
<plugins>
<!-- Config: Maven Tomcat Plugin -->
<!-- http://mvnrepository.com/artifact/org.apache.tomcat.maven/tomcat7-maven-plugin -->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<!-- Config: contextPath and Port (Default - /SimpleProject : 8080) -->
<!--
<configuration>
<path>/</path>
<port>8899</port>
</configuration>
-->
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
Why I'm getting this error? 为什么我会收到此错误?
FilteLogin FilteLogin
package it.tgi.cd.servlet;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebFilter(urlPatterns = {"/index.jsp", "/insert.jsp", "/search.jsp", "/addSongs.jsp", "/delete.jsp"}, filterName = "FilterLogin")
public class FilterLogin implements javax.servlet.Filter {
public void destroy() {
}
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) resp;
if(request.getSession().getAttribute("user") != null) {
chain.doFilter(req, resp);
} else {
response.sendRedirect("/login.jsp");
}
}
public void init(FilterConfig config) throws ServletException {
}
}
I used "provide" as suggested and the exception disappear but now when I try to go to "localhost ecc.." there is an exception 我按照建议使用“ provide”,但该异常消失了,但是现在当我尝试转到“ localhost ecc ..”时,出现了一个异常
INFORMAZIONI: Starting ProtocolHandler ["http-bio-8080"]
ott 04, 2016 3:06:42 PM org.apache.catalina.core.StandardWrapperValve invoke
GRAVE: Servlet.service() for servlet [jsp] in context with path [/CDStore] threw exception [java.lang.NullPointerException] with root cause
java.lang.NullPointerException
at org.apache.jsp.login_jsp._jspInit(login_jsp.java:31)
at org.apache.jasper.runtime.HttpJspBase.init(HttpJspBase.java:49)
at org.apache.jasper.servlet.JspServletWrapper.getServlet(JspServletWrapper.java:180)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:369)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
I don't know if it is normal but it remainsstuck on "INFORMAZIONI: Starting ProtocolHandler ["http-bio-8080"]" 我不知道这是否正常,但仍然停留在“ INFORMAZIONI:正在启动ProtocolHandler [“ http-bio-8080”]“上
Then I go to the localhost and the exception disappears. 然后我转到本地主机,异常消失。
Under WEB-INF/lib I didn't have any jar file. 在WEB-INF / lib下,我没有任何jar文件。 This is my build path
这是我的构建路径
1) Add the servlet dependency in pom.xml as below: 1)在pom.xml中添加servlet依赖关系,如下所示:
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
2) Make sure that your classpath is not populated with multiple different versions of javax.servlet.Filter
classes. 2)确保没有用多个不同版本的
javax.servlet.Filter
类填充您的类路径。
Check for servlet-api.jar
does exists in /WEB-INF/lib
folder. 检查
/WEB-INF/lib
文件夹中是否存在servlet-api.jar
。 you must add the dependency either in pom.xml
or in your build-path
. 您必须在
pom.xml
或build-path
添加依赖项。
You are packaging the javax.servlet-api
jar file with your application. 您正在将
javax.servlet-api
jar文件与您的应用程序打包在一起。 You should use the jar file available in Tomcat to avoid the ClassCastException. 您应该使用Tomcat中可用的jar文件来避免ClassCastException。
You can change the scope of the maven dependency to provided to not include the jar file in your application: 您可以将maven依赖项的范围更改为提供的范围,以在应用程序中不包含jar文件:
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.0-b01</version>
<scope>provided</scope>
</dependency>
Your answers were correct but the main problem was that I was trying to launch the web application with tomcat7-plugin even though I had another version of it. 您的回答是正确的,但是主要的问题是,即使我有另一个版本,我也尝试使用tomcat7-plugin启动Web应用程序。 So my bad knowledge of maven was saying that I had to run the web app with the plugin of tomcat (tomcat7-plugin in the pom.xml) because I thought that it was supposed to do so.
因此,我对maven的不良了解是说我必须使用tomcat插件(pom.xml中的tomcat7-plugin)运行Web应用程序,因为我认为应该这样做。
The project now work perfectly launching it from the tomcat run configuration. 现在,该项目可以完美地从tomcat运行配置启动它。 I'll gave ua thumbs up
我会举起大拇指
add an exclusion to the last library you added in your application: 将排除项添加到您在应用程序中添加的最后一个库中:
<dependency>
<groupId>com.netflix.eureka</groupId>
<artifactId>eureka-client</artifactId>
<version>1.8.0</version>
<scope>runtime</scope>
<exclusions>
<exclusion>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
</exclusion>
</exclusions>
</dependency>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.