javaweb 获取客户端请求头信息

public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
    	//设置将字符以“UTF-8”
		response.setCharacterEncoding("UTF-8");
		response.setHeader("content-type", "text/html;charset=UTF-8");
		PrintWriter out=response.getWriter();
		Enumeration reqHeadInfos=request.getHeaderNames();//获取所有的请求头
		out.write("获取到客户端所有的请求头信息如下");
		out.write("
"); while(reqHeadInfos.hasMoreElements()) { String headName=(String)reqHeadInfos.nextElement(); String headValue=request.getHeader(headName);//根据请求头的没给他脑子获取对应的请求头的 out.write(headName+":"+headValue); out.write("
"); } out.write("
"); out.write("获取到客户端Accept-Encoding请求头的值"); out.write("
"); String value=request.getHeader("Accept-Encoding"); out.write(value); Enumeration e=request.getHeaders("Accept-Encoding"); while(e.hasMoreElements()) { String string=(String) e.nextElement(); System.out.println(string); } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); }

效果如下
javaweb 获取客户端请求头信息_第1张图片

你可能感兴趣的:(javaWeb)