Search code examples
javaeclipsemavenservletsservlet-filters

The type getServletContext() is undefined for the type HttpServletRequest in a maven project for Eclipse


I was following an example online but when I use the certain function request.getServletContext().getServletRegistrations(). It does not recognize this function

 //   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;
}

When I use this same function in a regular dynamic web project it works fine with no errors, but using maven these same methods are undefined. I tried to change the request.getServletContext().getServletRegistrations();

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

I recognized the getSession.getServletContext(), but it then doesn't recognize the getServletRegistrations(). I figured it might've been a dependencies problem so I added several dependencies to my 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>

Any help would be greatly appreciated. Thank You.


Solution

  • Solution

    Replace :

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

    with

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

    Differenct between org.mortbay.jetty:servlet-api-2.5:6.1.5 and javax.servlet.jsp:servlet-api:2.1

    Jetty has a long and colorful history with jsp, having no jsp implementation of our own we have leveraged other implementations often, judging by the version numbers your looking at those are very old versions where we were maintaining patches on top of the glassfish jsp implementation. I think it was a patch for supporting logging in jetty and then a bug fix or three.

    Now a days we have been using the jsp artifacts from the java.net project which was spun out from glassfish a while back. However that doesn't seem to be tracking bug fixes very regularly either so we are kicking around trying the jasper implementation in tomcat.

    Back on your question, the jsp-api artifacts are typically just repackaged artifacts since the api doesn't change frequently. We historically rebundled them to keep them paired with the patched implementation.

    Now, you are obviously using a jetty-6 setup since your still using org.mortbay packaging but jetty6 and jetty7 are both servlet-api 2.5 so you might be able to get away with using the jetty7 jsp setup, we have a handy pom that declares these artifacts here:

    These are glassfish bundles as well, repackaged and made into osgi bundles in the process so they can be used with jetty in osgi environments....they ought to work normally though, we package them in our jetty7 distributions.