演示获取请求头信息

package com.javaweb.httprequest;
//演示获取请求头信息
import java.io.IOException;
import java.util.Enumeration;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * Servlet implementation class ServletRequestQingqiutou
 */
@WebServlet("/servletRequestQingqiutou")
public class ServletRequestQingqiutou extends HttpServlet {
private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ServletRequestQingqiutou() {
        super();
        // TODO Auto-generated constructor stub
    }


/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取单个请求头信息
String s=request .getHeader("Accept-Encoding");
System.out.println(s);
//获取同名的请求头的多个值
Enumerationenu=request.getHeaders("Accept-Encoding");
while (enu.hasMoreElements()) {
System.out.println(enu.nextElement());

}
//拿到所有的请求头和值
Enumeration enu1=request.getHeaderNames();
while (enu1.hasMoreElements()) {
String key=enu1.nextElement();
System.out.println(key+":"+request.getHeader(key));
}

}


/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}


}

你可能感兴趣的:(Javaweb)