Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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.

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

<?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

   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

   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"]"

Then I go to the localhost and the exception disappears.

Under WEB-INF/lib I didn't have any jar file. This is my build path

<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.
Check for servlet-api.jar does exists in /WEB-INF/libfolder. you must add the dependency either in pom.xml or in your build-path.

I used "provided" and the cast exception disappears, I put servlet-api.jar into web-inf/lib and then in "Dipendencies" (I use intellij) and now it gives me "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)" – Stefano Pisano Oct 4, 2016 at 12:10 this exception is different, put the entire stack trace in the question. remember there should be only one servlet-api jar either in lib folder or in pom.xml – Rohit Gaikwad Oct 4, 2016 at 13:07 a nullPointerException occurs when you try to access a variable which is not initialized. i think it occurs at ` response.sendRedirect("/login.jsp");` or if(request.getSession().getAttribute("user") != null) check the line no.31 code – Rohit Gaikwad Oct 4, 2016 at 13:25 This is theline 31 of login.java el_expressionfactory = _jspxFactory.getJspApplicationContext(getServletConfig().getServletContext()).getExpressionFactory(); – Stefano Pisano Oct 4, 2016 at 13:33

You are packaging the javax.servlet-api jar file with your application. You should use the jar file available in Tomcat to avoid the ClassCastException.

You can change the scope of the maven dependency to provided to not include the jar file in your application:

<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. 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.

The project now work perfectly launching it from the tomcat run configuration. I'll gave u a thumbs up

<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>

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.