HttpServlet响应变量未编译



我在编译以下代码时遇到问题

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String method = request.getParameter("method");
    System.out.println(response.toString());
    System.out.println(response);
    response.setHeader("header", "value");
    System.out.println(response.getHeader("header")); // This is line 103 and gives error
    // more logic here
}

基本上,我试图在响应对象中设置一个标头,我只想测试它是否成功设置,这就是我打印它的原因。

但是当我尝试用maven编译上面的代码时,它在下面给出了错误消息

[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 9.114 s
[INFO] Finished at: 2016-01-07T09:33:59+05:00
[INFO] Final Memory: 24M/312M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.3.2:compile (default-compile) on project inQuireCatalogWS: Compilation failure
[ERROR] service/catalog/SpexWidgetServlet.java:[103,35] error: cannot find symbol
[ERROR] -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.

令我困惑的是,maven能够很好地编译前面的行,只抱怨我调用getHeader()方法的行中的变量。我是不是遗漏了什么?

编辑

我的环境低于

Apache Maven 3.3.3 (7994120775791599e205a5524ec3e0dfe41d4a06; 2015-04-22T16:57:37+05:00)
Maven home: /opt/apache-maven-3.3.3
Java version: 1.8.0_45, vendor: Oracle Corporation

pom.xml中,servlet-api的条目如下所示

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

这是因为HttpServletResponse在Servlet API 3.0 之前没有方法getHeader()

不过你可以查一下containsHeader ()。那已经存在很长时间了。或者简单地更新到Servlet 3.0-如果您的容器支持:

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

最新更新