Java tomcat未发送请求的http头
我在Java tomcat未发送请求的http头,java,tomcat,http-headers,Java,Tomcat,Http Headers,我在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_addH
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
但是我得到了
null
,所以如果我试图打印任何标题元素,结果得到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>
服务器清晰地打印出来
Host: localhost:8080
在您的例子中,服务器以302响应,因此您的servlet永远不会被命中。我不知道你的null
头来自哪里
不要使用
cURL
,而是尝试使用Java HTTP客户端,例如Apache的HttpClient
是否cURL
发送Host
头?@SotiriosDelimanolis:我更新了问题。哪个servlet正在尝试获取头?你是用302还是容器来响应?@SotiriosDelimanolis:It's themy app
servlet作为tomcat
根目录,我只是执行curl,但在标题中什么也没有。请发布最小化的servlet代码、web.xml
和应用程序的上下文路径。我们一定遗漏了什么。它命中了servlet,因为我可以在doGet
方法中获取所有调试消息302
是因为vController
@stsd中的url重定向导致的问题是什么?
<%@ 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>
$ 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