IDEA找不到程序包 和 request.getServletContext()报错Cannot resolve method 'getServletContext()的解决方法【转载自https://blog.csdn.net/lwh156541064/article/details/94394871】

重新装了idea和down了项目却一直报错,在调用request.getServletContext()的方法时一直报Cannot resolve method 'getServletContext()的错误,网上查了好多方法,大多数都是在说是servlet3.0才可以支持此方法,而servlet3.0只有tomcat7以上版本才可以,我检查了我自己的tomcat发现就是3.0,最终找到原因

对于Maven项目要检查pom文件中的servlet引入的是哪个包,我原来写的是这个

    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>servlet-api</artifactId>
      <version>2.5</version>
    </dependency>

是2.5版本的,所以一直报错,需要改成这个

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

而且加上这个的时候 前面2.5版本的需要注释掉或者删掉,不然还是会引用前面的那个

原文地址:https://www.cnblogs.com/KeepZ/p/13166862.html