gpt4 book ai didi

java - HttpServletResponse 变量未编译

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:40:10 26 4
gpt4 key购买 nike

编译以下代码时遇到问题

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
}

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

但是当我尝试用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 没有 方法 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>

关于java - HttpServletResponse 变量未编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34647512/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com