【发布时间】:2013-09-02 15:15:11
【问题描述】:
我在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 中,我尝试从标头打印 Host 元素:
String host = request.getHeader("Host");
System.out.println("Host: "+host);
但我得到null,所以如果我尝试打印任何标题元素,我会得到null。有什么帮助吗?
【问题讨论】:
-
curl是否发送Host标头? -
@SotiriosDelimanolis:我更新了问题。
-
哪个 servlet 试图获取标头?你是用 302 还是容器响应?
-
@SotiriosDelimanolis:它的
my-appservlet 被放置为tomcat根目录,我只是执行 curl 但我在标题中什么也没有...... -
请发布最小化的 servlet 代码、
web.xml和应用程序的上下文路径。我们一定错过了什么。
标签: java tomcat http-headers