servlet 2.1 例子:查看请求头

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>");
    }

打开网页显示的结果

如: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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容