如何使用 JSP 读取 HTTP 头?
以下是使用 HttpServletRequest 的 getHeaderNames() 方法读取 HTTP 头信息的示例。此方法返回一个枚举,其中包含与当前 HTTP 请求关联的头信息。
在获得一个枚举后,我们可以按标准方式向下循环枚举。我们将使用 hasMoreElements() 方法确定何时停止,并使用 nextElement() 方法获取每个参数名称的名称。
<%@ page import = "java.io.*,java.util.*" %>
<html>
<head>
<title>HTTP Header Request Example</title>
</head>
<body>
<center>
<h2>HTTP Header Request Example</h2>
<table width = "100%" border = "1" align = "center">
<tr bgcolor = "#949494">
<th>Header Name</th>
<th>Header Value(s)</th>
</tr>
<%
Enumeration headerNames = request.getHeaderNames();
while(headerNames.hasMoreElements()) {
String paramName = (String)headerNames.nextElement();
out.print("<tr><td>" + paramName + "</td>
");
String paramValue = request.getHeader(paramName);
out.println("<td> " + paramValue + "</td></tr>
");
}
%>
</table>
</center>
</body>
</html>现在,我们将上面的代码放入 main.jsp 中并尝试访问它。
HTTP 头请求示例
| 头名称 | 头值 |
|---|---|
| 接受 | */* |
| 接受语言 | en-us |
| 使用者代理 | Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; InfoPath.2; MS-RTC LM 8) |
| 接受编码 | gzip, deflate |
| 主机 | localhost:8080 |
| 连接 | 保持活动 |
| 缓存控制 | 不缓存 |
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP