error: package javax.servlet does not exist

前端 未结 6 603
鱼传尺愫
鱼传尺愫 2020-12-01 01:39

I was trying to implement a login filter in my web app with jsf 2, following this guide:

https://stackoverflow.com/tags/servlet-filters/info

after I compiled

相关标签:
6条回答
  • 2020-12-01 02:12

    maybe doesnt exists javaee-api-7.0.jar. download this jar and on your project right clik

    1. on your project right click
    2. build path
    3. Configure build path
    4. Add external Jars
    5. javaee-api-7.0.jar choose
    6. Apply and finish
    0 讨论(0)
  • 2020-12-01 02:15

    I only put this code in my pom.xml and I executed the command maven install.

    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.0.1</version>
        <scope>provided</scope>
    </dependency>
    
    0 讨论(0)
  • 2020-12-01 02:18

    The javax.servlet dependency is missing in your pom.xml. Add the following to the dependencies-Node:

    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.0.1</version>
        <scope>provided</scope>
    </dependency>
    
    0 讨论(0)
  • 2020-12-01 02:20

    I needed to import javaee-api as well.

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

    Unless I got following error:

    package javax.servlet.http does not exist
    javax.servlet.annotation does not exist
    javax.servlet.http does not exist
    ...
    
    0 讨论(0)
  • 2020-12-01 02:21

    The answer provided by @Matthias Herlitzius is mostly correct. Just for further clarity.

    The servlet-api jar is best left up to the server to manage see here for detail

    With that said, the dependency to add may vary according to your server/container. For example in Wildfly the dependency would be

    <dependency>
        <groupId>org.jboss.spec.javax.servlet</groupId>
        <artifactId>jboss-servlet-api_3.1_spec</artifactId>
        <scope>provided</scope>
    </dependency>
    

    So becareful to check how your container has provided the servlet implementation.

    0 讨论(0)
  • 2020-12-01 02:29

    In my case, migrating a Spring 3.1 app up to 3.2.7, my solution was similar to Matthias's but a bit different -- thus why I'm documenting it here:

    In my POM I found this dependency and changed it from 6.0 to 7.0:

        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-web-api</artifactId>
            <version>7.0</version>
            <scope>provided</scope>
        </dependency>
    

    Then later in the POM I upgraded this plugin from 6.0 to 7.0:

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.1</version>
                <executions>
                    <execution>
                        ...
                        <configuration>
                            ...
                            <artifactItems>
                                <artifactItem>
                                    <groupId>javax</groupId>
                                    <artifactId>javaee-endorsed-api</artifactId>
                                    <version>7.0</version>
                                    <type>jar</type>
                                </artifactItem>
                            </artifactItems>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
    
    0 讨论(0)
提交回复
热议问题