java - Eclipse 的 maven 项目中的 HttpServletRequest 类型未定义类型 getServletContext()

标签 java eclipse maven servlets servlet-filters

我正在网上遵循一个示例,但是当我使用某个函数 request.getServletContext().getServletRegistrations() 时。它不识别此功能

 //   Check the target of the request is a servlet?
 private boolean needJDBC(HttpServletRequest request) {
   System.out.println("JDBC Filter");

   String servletPath = request.getServletPath();
   // => /abc/mnp
   String pathInfo = request.getPathInfo();

   String urlPattern = servletPath;

   if (pathInfo != null) {
       // => /spath/*
       urlPattern = servletPath + "/*";
   }

   // Key: servletName.
   // Value: ServletRegistration

   Map<String, ? extends ServletRegistration> servletRegistrations = request.getServletContext()
           .getServletRegistrations();

   // Collection of all servlet in your webapp.
   Collection<? extends ServletRegistration> values = servletRegistrations.values();
   for (ServletRegistration sr : values) {
       Collection<String> mappings = sr.getMappings();
       if (mappings.contains(urlPattern)) {
           return true;
       }
   }
   return false;
}

当我在常规动态 Web 项目中使用相同的函数时,它工作正常,没有错误,但使用 Maven 时,这些相同的方法是未定义的。我试图改变 request.getServletContext().getServletRegistrations();

至 request.getSession().getServletContext().getServletRegistrations();

我识别出了 getSession.getServletContext(),但它无法识别 getServletRegistrations()。我认为这可能是一个依赖项问题,因此我在 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>com.ers</groupId>
<artifactId>ERSProject</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>ERSProject Maven Webapp</name>
<url>http://maven.apache.org</url>


<repositories>
    <!-- Repository for ORACLE ojdbc6. -->
    <repository>
        <id>codelds</id>
        <url>https://code.lds.org/nexus/content/groups/main-repo</url>
    </repository>
</repositories>



<dependencies>

    <!-- Oracle database driver -->
    <dependency>
        <groupId>com.oracle</groupId>
        <artifactId>ojdbc6</artifactId>
        <version>11.2.0.3</version>
    </dependency>

    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>3.8.1</version>
        <scope>test</scope>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.glassfish.web/javax.servlet.jsp.jstl -->
    <dependency>
        <groupId>org.glassfish.web</groupId>
        <artifactId>javax.servlet.jsp.jstl</artifactId>
        <version>1.2.5-b03</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/javax.servlet.jsp.jstl/javax.servlet.jsp.jstl-api -->
    <dependency>
        <groupId>javax.servlet.jsp.jstl</groupId>
        <artifactId>javax.servlet.jsp.jstl-api</artifactId>
        <version>1.2.1</version>
    </dependency>

    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.0.1</version>
        <scope>provided</scope>
    </dependency>

</dependencies>
<build>
    <finalName>ERSProject</finalName>
</build>

任何帮助将不胜感激。谢谢。

最佳答案

解决方案

替换:

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>3.0.1</version>
    <scope>provided</scope>
</dependency>

</dependency>
    </groupId>`org.mortbay`.jetty</groupId>
    </artifactId>servlet-api</artifactId>
    </version>3.0.20100224</version>
</dependency>

org.mortbay.jetty:servlet-api-2.5:6.1.5之间的差异和javax.servlet.jsp:servlet-api:2.1

Jetty 在 jsp 方面有着悠久而丰富多彩的历史,我们自己没有 jsp 实现,我们经常利用其他实现,从版本号来看,您看到的那些都是非常旧的版本,我们在 glassfish 上维护补丁jsp实现。我认为这是一个支持 jetty 登录的补丁,然后是一个或三个错误修复。

现在,我们一直在使用 java.net 项目中的 jsp Artifact ,该项目是不久前从 glassfish 中分离出来的。然而,这似乎也没有定期跟踪错误修复,因此我们正在尝试在 tomcat 中使用 jasper 实现。

回到你的问题,jsp-api Artifact 通常只是重新打包的 Artifact ,因为 api 不会经常更改。我们过去曾将它们重新捆绑,以使它们与修补后的实现配对。

现在,您显然正在使用 jetty-6 设置,因为您仍在使用 org.mortbay包装但jetty6和jetty7都是servlet-api 2.5所以你也许可以使用 jetty7 来逃脱惩罚jsp 设置,我们有一个方便的 pom,在这里声明这些 Artifact :

这些也是 glassfish bundle ,在此过程中重新打包并制成 osgi bundle ,以便它们可以在 osgi 环境中与 jetty 一起使用......不过它们应该可以正常工作,我们将它们打包在 jetty7 中。分布。

关于java - Eclipse 的 maven 项目中的 HttpServletRequest 类型未定义类型 getServletContext(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41325027/

相关文章:

java - 按下按钮时更改 XML 布局

java - 无法处理 XML。获取 不允许匹配 "[xX][mM][lL]"的处理指令目标。 org.xml.sax.SAXParseException 错误

java - Java 中不显示多边形形状

maven - JUnit5 平台启动器到底是什么?

java - 通过命令行使用mvn运行Java程序

java - Hibernate 无参数构造函数可见性

java - openjfx 是否已在 OpenJDK 中合并?

java - 将 webapp 转换为 war 文件时覆盖现有源文件意味着什么

java - Google App Engine - Blobstore 请求用于生产而不是开发

maven - WAR 文件中缺少编译的类