当前位置: 首页 > 知识库问答 >
问题:

错误:包javax.servlet不存在

晏阳飙
2023-03-14

我正试图用jsf 2在我的web应用程序中实现一个登录过滤器,遵循以下指南:

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

在我编译了我的过滤器并添加了. class在web-inf/class(如指南所说)过滤器工作,但我把错误的url重定向到登录页面,所以我删除了文件夹中的filter.class(web-inf/class),并试图再次编译该项目,但失败了,从那以后,我得到了“包javax.servlet不存在”

这很奇怪,因为在它工作之前,我有javax。在我的pom中。xml。。我试着清理这个项目,但什么都没有。

这是我的过滤类:

package Bean;

import java.io.IOException;

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;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Created with IntelliJ IDEA.
 * User: rodrigo
 * Date: 28-04-13
 * Time: 06:54 AM
 * To change this template use File | Settings | File Templates.
 */
@WebFilter("/Contenido/*")
public class filtro implements Filter {

    @Override
    public void init(FilterConfig config) throws ServletException {
        // If you have any <init-param> in web.xml, then you could get them
        // here by config.getInitParameter("name") and assign it as field.
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response,
            FilterChain chain) throws ServletException, IOException {
        HttpServletRequest req = (HttpServletRequest) request;
        LoginBean user = (LoginBean) req.getSession().getAttribute("user");

        if (user != null && user.isLoggedIn()) {
            // User is logged in, so just continue request.
            chain.doFilter(request, response);
        } else {
            // User is not logged in, so redirect to index.
            HttpServletResponse res = (HttpServletResponse) response;
            res.sendRedirect(req.getContextPath() + "/Contenido/Login.xhtml");
        }
    }

    @Override
    public void destroy() {
        // If you have assigned any expensive resources as field of
        // this Filter class, then you could clean/close them here.
    }
}

这就是错误:

\Users\rodrigo\IdeaProjects\Frutemu\src\main\java\Bean\filtro.java:[5,20] error: package javax.servlet does not exist
[ERROR] \Users\rodrigo\IdeaProjects\Frutemu\src\main\java\Bean\filtro.java:[6,20] error: package javax.servlet does not exist
[ERROR] \Users\rodrigo\IdeaProjects\Frutemu\src\main\java\Bean\filtro.java:[7,20] error: package javax.servlet does not exist
[ERROR] \Users\rodrigo\IdeaProjects\Frutemu\src\main\java\Bean\filtro.java:[8,20] error: package javax.servlet does not exist
[ERROR] \Users\rodrigo\IdeaProjects\Frutemu\src\main\java\Bean\filtro.java:[9,20] error: package javax.servlet does not exist
[ERROR] \Users\rodrigo\IdeaProjects\Frutemu\src\main\java\Bean\filtro.java:[10,20] error: package javax.servlet does not exist
[ERROR] \Users\rodrigo\IdeaProjects\Frutemu\src\main\java\Bean\filtro.java:[11,31] error: package javax.servlet.annotation does not exist

我的pom。xml:

<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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>Frutemu</groupId>
    <artifactId>Frutemu</artifactId>
    <packaging>war</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>Frutemu Maven Webapp</name>
    <url>http://maven.apache.org</url>
    <repositories>
        <repository>
            <id>prime-repo</id>
            <name>Prime Repo</name>
            <url>http://repository.primefaces.org</url>
        </repository>
    </repositories>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>javax.ejb</groupId>
            <artifactId>ejb-api</artifactId>
            <version>3.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.primefaces</groupId>
            <artifactId>primefaces</artifactId>
            <version>3.5</version>
        </dependency>
        <dependency>
            <groupId>org.primefaces.themes</groupId>
            <artifactId>all-themes</artifactId>
            <version>1.0.9</version>
        </dependency>
        <dependency>
            <groupId>javax.faces</groupId>
            <artifactId>jsf-api</artifactId>
            <version>2.0</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>com.sun.faces</groupId>
            <artifactId>jsf-impl</artifactId>
            <version>2.0.2-b10</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
        <!-- MySQL database driver -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.21</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.web</groupId>
            <artifactId>el-impl</artifactId>
            <version>2.2.1-b04</version>
            <scope>provided</scope>
        </dependency>
        <!-- OpenJPA framework -->
        <dependency>
            <groupId>org.apache.openjpa</groupId>
            <artifactId>openjpa-all</artifactId>
            <version>2.2.0</version>
        </dependency>
        <dependency>
            <groupId>com.microsoft.sqlserver</groupId>
            <artifactId>sqljdbc4</artifactId>
            <version>4.0</version>
        </dependency>
        <dependency>
            <groupId>net.sf.jasperreports</groupId>
            <artifactId>jasperreports</artifactId>
            <version>4.1.2</version>
        </dependency>
    </dependencies>
    <build>
        <finalName>Frutemu</finalName>
        <plugins>
            <!-- Open Jpa -->
            <plugin>
                <groupId>org.apache.openjpa</groupId>
                <artifactId>openjpa-maven-plugin</artifactId>
                <version>2.2.0</version>
                <configuration>
                    <includes>**/model/*.class</includes>
                    <addDefaultConstructor>true</addDefaultConstructor>
                    <enforcePropertyRestrictions>true</enforcePropertyRestrictions>
                </configuration>
                <executions>
                    <execution>
                        <id>enhancer</id>
                        <phase>process-classes</phase>
                        <goals>
                            <goal>enhance</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <!-- Plugin para levantar una instancia de Tomcat 7 liviana, única para este proyecto -->
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>2.0</version>
                <configuration>
                    <url>http://127.0.0.1:8080/manager/text</url>
                    <server>TomcatServer</server>
                    <path>/Frutemu</path>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>jasperreports-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>compile-reports</goal>
                        </goals>
                    </execution>
                </executions>
                <dependencies>
                    <!--note this must be repeated here to pick up correct xml validation -->
                    <dependency>
                        <groupId>net.sf.jasperreports</groupId>
                        <artifactId>jasperreports</artifactId>
                        <version>4.1.2</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>
</project>

共有3个答案

魏鹤轩
2023-03-14

@Matthias Herlitzius提供的答案基本上是正确的。只是为了进一步澄清。

servlet api jar最好由服务器来管理,详情请参见此处

尽管如此,要添加的依赖项可能会因服务器/容器而异。例如,在Wildfly中,依赖关系是

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

所以要小心检查容器是如何提供servlet实现的。

全弘深
2023-03-14

我只把这个代码放在我的pom里。xml和我执行了maven install命令。

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>3.0.1</version>
    <scope>provided</scope>
</dependency>
乐山
2023-03-14

pom.xml.中缺少javax.servlet依赖项将以下内容添加到依赖项节点:

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>3.0.1</version>
    <scope>provided</scope>
</dependency>
 类似资料:
  • 问题内容: 我有一个包,其中导入了javax.servlet。和javax.servlet.http。,当我尝试在命令提示符下对其进行编译时,出现错误 软件包javax.servlet不存在 我使用JDK 1.7.0和Tomcat 6.0。 问题答案: 您需要将Tomcat 文件的路径添加到编译时类路径。 Java需要在类路径中查找导入的依赖项。否则,它将默认为上面示例中包含的当前文件夹。的是Wi

  • 包javax.servlet不存在 我使用JDK1.7.0和Tomcat6.0。

  • 问题内容: 我有一个目录结构,例如在包含Java文件的根目录下。我在与上述相同的根目录中的目录结构中还有另一个Java文件。我正在从中调用方法,但收到错误消息,该程序包不存在。 我已经在我的java文件中导入了包。能给我一些建议吗? 问题答案: 这有效: com / example / model / BearExtra.java com / example / web / Bear.java 现

  • 问题内容: 当我尝试构建我的android项目时,出现此错误 错误:(8,37)错误:包android.support.design.widget不存在错误:(18,9)错误:找不到符号类TabLayout错误:(18,32)错误:找不到符号类TabLayout错误: (21,33)错误:找不到符号变量TabLayout错误:(27,56)错误:软件包TabLayout不存在错误:(48,36)错

  • 我能够在本地机器上构建java项目,它使用maven成功构建。然而,当我在Jenkins机器上构建它时,我得到了一个编译错误: 包javax.jms不存在 这是什么意思?它在哪里寻找javax。jms?在本地m2回购中,classpath? 我不得不对我的pom进行以下更改。xml使其在Linux上工作: a)明确声明maven-site-plugin版本为2.1。org.apache.maven

  • 我正在使用jid3库从mp3文件中提取信息。我已经下载了jar并将其添加到BlueJ库中。 我的导入线如下 虽然我越来越 我在谷歌上搜索了一段时间,似乎找不到问题所在,有人有什么想法吗?这与这里的库示例中使用的导入行相同 正如图书馆文件所示。 任何帮助将不胜感激,谢谢。