1、代码报错
2、解决方案
2.1、使用servlet-api的2.5版本时
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>compile</scope>
</dependency>
代码中最后是通过getSession()方法获取,在HttpSession中是有getServletContext()方法的。而不能直接通过request去获取getServletContext()
ServletContext servletContext = request.getSession().getServletContext();
2.2、使用servlet-api的3.1版本时
<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
代码中可以直接通过request获取getServletContext()
request.getServletContext()