0

我有一个servlet部署在tomcat/webapps目录下,我尝试向它发送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结果。有什么帮助吗?

4

1 回答 1

0

在 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标题来自哪里。

而不是 with cURL,尝试使用 Java HTTP 客户端,例如。阿帕奇的HttpClient.

于 2013-09-02T16:42:37.203 回答