Java 怎么在servlet中获取请求头(Request Header)

2019年8月16日22:05:59Java 怎么在servlet中获取请求头(Request Header)已关闭评论 261 819字阅读2分43秒

1.简介

Java 怎么在servlet中获取请求头(Request Header)?——请看下文。

2.示例代码

/**
 * 版权所有 编程十万个怎么办(www.tah1986.com)
 */

public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    doGetOrPost(req, resp);
}

public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    doGetOrPost(req, resp);
}

private void doGetOrPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    PrintWriter out = resp.getWriter();
    resp.setContentType("text/plain");

    
    String name = "user-agent";
    String value = req.getHeader(name);
    if (value == null) {
        
    }
    
    Enumeration enum = req.getHeaderNames();
    for (; enum.hasMoreElements(); ) {
        
        name = (String)enum.nextElement();
        out.println(name);

        
        value = req.getHeader(name);

        Enumeration valuesEnum = req.getHeaders(name);
        for (; valuesEnum.hasMoreElements(); ) {
            
            value = (String)valuesEnum.nextElement();

            out.println("    "+value);
        }
    }
    out.close();
}

 
继续阅读
编程十万个怎么办