编译以下代码时遇到问题
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>
答案 0 :(得分:3)
那是因为HttpServletResponse
只是没有 方法getHeader()
until Servlet API 3.0
您可以检查containsHeader ()
。那是很久了。或者只是更新到Servlet 3.0 - 如果您的容器支持:
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>3.0</version>
<scope>provided</scope>
</dependency>