gpt4 book ai didi

java - tomcat 没有发送请求的 http-header

转载 作者:行者123 更新时间:2023-11-28 23:42:16 25 4
gpt4 key购买 nike

我在 tomcat/webapps 目录下部署了一个 servlet,我尝试向它发送一个 curl 请求:

 $~ curl -v http://localhost:8080/

* About to connect() to localhost port 8080 (#0)
* Trying 127.0.0.1...
* Adding handle: conn: 0x33e0ee0
* Adding handle: send: 0
* Adding handle: recv: 0
* Curl_addHandleToPipeline: length: 1
* - Conn 0 (0x33e0ee0) send_pipe: 1, recv_pipe: 0
* Connected to localhost (127.0.0.1) port 8080 (#0)
> GET / HTTP/1.1
> User-Agent: curl/7.30.0
> Host: localhost:8080
> Accept: */*
>
< HTTP/1.1 302 Found
* Server Apache-Coyote/1.1 is not blacklisted
< Server: Apache-Coyote/1.1
< Set-Cookie: JSESSIONID=<ID>; Path=/; HttpOnly
< Location: http://localhost:8080/
< Content-Type: text/html;charset=EUC-KR
< Content-Length: 0
< Date: Mon, 02 Sep 2013 14:24:53 GMT
<
* Connection #0 to host localhost left intact

这是 web.xml 文件:

     ...
<servlet>
<description></description>
<display-name>vController</display-name>
<servlet-name>vController</servlet-name>
<servlet-class>com.vc.cac.controller.vlController</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>vController</servlet-name>
<url-pattern>/vController</url-pattern>
</servlet-mapping>
<listener>
<listener-class>com.vc.cac.controller.vlController</listener-class>
</listener>
...

在 servlet 中,我尝试从 header 打印 Host 元素:

String host = request.getHeader("Host");
System.out.println("Host: "+host);

但我得到的是 null,所以如果我尝试打印任何 header 元素,我得到的结果是 null。有帮助吗?

最佳答案

在 Tomcat 7.0.22 上,以下 servlet

@WebServlet(urlPatterns = "/vController")
public class vlController extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse resp) throws ServletException, IOException {
String host = request.getHeader("Host");
System.out.println("Host: "+host);
resp.getWriter().write("host printed");
}
}

使用 index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<jsp:forward page="/vController" />
Heello wRoldasdas
</body>
</html>

cURL 命令打印

$ clear; curl -v http://localhost:8080/
* About to connect() to localhost port 8080 (#0)
* Trying 127.0.0.1...
* 0x60002d0f0 is at send pipe head!
* STATE: CONNECT => WAITCONNECT handle 0x60006cac0; line 1032 (connection #0)
* Connected to localhost (127.0.0.1) port 8080 (#0)
* STATE: WAITCONNECT => DO handle 0x60006cac0; line 1151 (connection #0)
> GET / HTTP/1.1
> User-Agent: curl/7.29.0
> Host: localhost:8080
> Accept: */*
>
* STATE: DO => DO_DONE handle 0x60006cac0; line 1236 (connection #0)
* STATE: DO_DONE => WAITPERFORM handle 0x60006cac0; line 1352 (connection #0)
* STATE: WAITPERFORM => PERFORM handle 0x60006cac0; line 1363 (connection #0)
* HTTP 1.1 or later with persistent connection, pipelining supported
< HTTP/1.1 200 OK
HTTP/1.1 200 OK
< Server: Apache-Coyote/1.1
Server: Apache-Coyote/1.1
< Set-Cookie: JSESSIONID=7694BACEC0C311357E72DCEA7574540A; Path=/; HttpOnly
Set-Cookie: JSESSIONID=7694BACEC0C311357E72DCEA7574540A; Path=/; HttpOnly
< Content-Type: text/html;charset=ISO-8859-1
Content-Type: text/html;charset=ISO-8859-1
< Content-Length: 12
Content-Length: 12
< Date: Mon, 02 Sep 2013 16:40:11 GMT
Date: Mon, 02 Sep 2013 16:40:11 GMT

<
* STATE: PERFORM => DONE handle 0x60006cac0; line 1533 (connection #0)
* Connection #0 to host localhost left intact
host printed

并且服务器清楚地打印

Host: localhost:8080

在您的情况下,服务器以 302 响应,因此您的 servlet 永远不会被命中。我不知道您的 null header 来自何处。

不使用 cURL,尝试使用 Java HTTP 客户端,例如。 Apache 的 HttpClient

关于java - tomcat 没有发送请求的 http-header,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18576401/

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