html文件
<!DOCTYPE html>
<html >
<head>
</head>
<body >
<h2>header display</h2>
<form method = "get" action = "HeaderDisplay">
<input type = "submit" value = "headerDisplay"/>
</form>re</body>
</html>
doGet方法體
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
out.println("<html><body>");
out.print("請求頭消息:<br/>");
for(String name : Collections.list(request.getHeaderNames())){
out.printf("<b>%s :</b> %s\n<br/>", name, request.getHeader(name));
}
out.println("</body></html>");
}
打開網(wǎng)頁顯示的結(jié)果
如:http://localhost:8080/servletDemo/HeaderDisplay
請求頭消息:
host : localhost:8080
connection : keep-alive
cache-control : max-age=0
upgrade-insecure-requests : 1
user-agent : Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36
accept : text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,/;q=0.8
referer : http://localhost:8080/servletDemo/login.html
accept-encoding : gzip, deflate, sdch, br
accept-language : zh-CN,zh;q=0.8