java 获取HTTP 头部信息

java 获取HTTP 头部信息


public class TestHttpHeader extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {



response.setContentType("text/html");

PrintWriter out = response.getWriter();

Enumeration names = request.getHeaderNames();

while(names.hasMoreElements()){

String name = (String)names.nextElement();

out.println("<b>" + name + ":</b>" + request.getHeader(name) + "<br />"); if(name.equals("user-agent")){


out.println("<a href='#'>"+name+":"+request.getHeader(name)+"</a><br />");


}

System.err.println(name + ":" + request.getHeader(name) + "");

}

out.flush();

out.close();


}


}


web.xml



  <servlet>

    <description>This is the description of my J2EE component</description>

    <display-name>This is the display name of my J2EE component</display-name>

    <servlet-name>TestHttpHeader</servlet-name>

    <servlet-class>com.TestHttpHeader</servlet-class>

  </servlet>


  <servlet-mapping>

    <servlet-name>TestHttpHeader</servlet-name>

    <url-pattern>/servlet/TestHttpHeader</url-pattern>

  </servlet-mapping>


访问地址:


http://localhost:8080/httpHeader/servlet/TestHttpHeader

输出:


host:localhost:8080

connection:keep-alive

user-agent:Mozilla/5.0 (Windows NT 5.1) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.142 Safari/535.19

user-agent:Mozilla/5.0 (Windows NT 5.1) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.142 Safari/535.19

accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8

accept-encoding:gzip,deflate,sdch

accept-language:zh-CN,zh;q=0.8

accept-charset:GBK,utf-8;q=0.7,*;q=0.3

-------------------------


http://www.eshow365.cn

 

你可能感兴趣的:(java,httpHeader)