在本章中,我们将在jsp中讨论和学习客户端请求。当浏览器请求网页时,它会向web服务器发送大量信息。这些信息不能直接读取,因为这些信息作为http请求标头的一部分。可以通过http协议以了解更多信息。
下表列出了浏览器中的重要http请求头信息。这些信息经常在web编程中使用 -
| 1 | accept | 此请求头指定浏览器或其他客户端可以处理的mime类型。 image/png或image/jpeg是两种最常见的值。 |
| 2 | accept-charset | 此请求头指定浏览器可用于显示信息的字符集。 例如:iso-8859-1,utf-8和gb2312等。 |
| 3 | accept-encoding | 此请求头指定浏览器知道如何处理的编码类型。gzip或compress是两种最常见的值。 |
| 4 | accept-language | 此请求头指定客户端的首选语言,以防servlet可以使用多种语言生成结果。例如:en,en-us,ru等 |
| 5 | authorization | 访问受密码保护的网页时,客户端使用此请求头来标识自己。 |
| 6 | connection | 此请求头指示客户端是否可以处理持久的http连接。 持久连接允许客户端或其他浏览器通过单个请求检索多个文件。如果是keep-alive值表示使用持久连接。 |
| 7 | content-length | 此请求头仅适用于post请求,并以字节为单位给出post数据的大小。 |
| 8 | cookie | 此请求头将返回给先前发送到服务器的cookie到浏览器。 |
| 9 | host | 此请求头指定原始url中给出的主机和端口。 |
| 10 | if-modified-since | 此请求头表示客户端仅在指定日期之后更改页面。 服务器发送代码304,表示如果没有较新的结果可用,则不修改标题。 |
| 11 | if-unmodified-since | 该请求头与if-modified-since相反; 它指定只有当文档比指定日期更早时,动作才能成功。 |
| 12 | referer | 此请求头指示引荐网页的url。 例如,如果位于网页a并单击链接到网页b,则当浏览器请求网页b时,网页a的url包含在referer标头中。 |
httpservletrequest对象
request对象是javax.servlet.http.httpservletrequest对象的一个实例。 每当客户端请求页面时,jsp引擎将创建一个新对象来表示该请求。
请求对象提供了获取包括表单数据,cookie,http方法等http头信息的方法。
下表列出了可用于在jsp程序中读取http头的重要方法。 httpservletrequest对象可以使用这些方法,该对象代表web服务器的客户端请求。
| 1 | cookie[] getcookies() | 返回一个数组,其中包含客户端使用此请求发送的所有cookie对象。 |
| 2 | enumeration getattributenames() | 返回枚举,其中包含此请求可用属性的名称。 |
| 3 | enumeration getheadernames() | 返回此请求包含的所有头名称的枚举。 |
| 4 | enumeration getparameternames() | 返回此请求中包含的参数名称的string对象的枚举。 |
| 5 | httpsession getsession() | 返回与此请求相关联的当前会话,或者如果请求没有会话,则自动创建一个会话。 |
| 6 | httpsession getsession(boolean create) | 返回与此请求相关联的当前httpsession,或者如果没有当前会话,并且create为true,则返回创建一个新会话。 |
| 7 | locale getlocale() | 根据accept-language标头返回客户端将接受内容的首选区域设置。 |
| 8 | object getattribute(string name) | 作为object返回命名属性的值,如果不存在给定名称的属性,则返回null。 |
| 9 | servletinputstream getinputstream() | 使用servletinputstream将请求的正文作为二进制数据检索。 |
| 10 | string getauthtype() | 返回用于保护servlet的认证方案的名称,例如“basic”或“ssl”,如果jsp未受保护,则返回null。 |
| 11 | string getcharacterencoding() | 返回此请求正文中使用的字符编码的名称。 |
| 12 | string getcontenttype() | 返回请求主体的mime类型,如果类型不知道,则返回null。 |
| 13 | string getcontextpath() | 返回指示请求上下文的请求uri的部分。 |
| 14 | string getheader(string name) | 以string形式返回指定的请求头的值。 |
| 15 | string getmethod() | 返回发出此请求的http方法的名称,例如:get,post或put。 |
| 16 | string getparameter(string name) | 以string形式返回请求参数的值,如果该参数不存在,则返回null。 |
| 17 | string getpathinfo() | 返回与客户端发出此请求时发送的url相关联的任何额外的路径信息。 |
| 18 | string getprotocol() | 返回请求使用的协议的名称和版本。 |
| 19 | string getquerystring() | 返回路径后请求url中包含的查询字符串。 |
| 20 | string getremoteaddr() | 返回发送请求的客户端的internet协议(ip)地址。 |
| 21 | string getremotehost() | 返回发送请求的客户端的完全限定名称。 |
| 22 | string getremoteuser() | 如果用户已通过身份验证,则返回进行此请求的用户的登录名,如果用户未通过身份验证,则返回null。 |
| 23 | string getrequesturi() | 将该请求url的一部分从协议名称返回到http请求的第一行中的查询字符串。 |
| 24 | string getrequestedsessionid() | 返回客户端指定的会话id。 |
| 25 | string getservletpath() | 返回调用jsp的请求的url部分。 |
| 26 | string[] getparametervalues(string name) | 返回包含给定请求参数的所有值的string对象数组,如果该参数不存在,则返回null。 |
| 27 | boolean issecure() | 返回一个布尔值,指示此请求是否使用安全通道(如https)进行通信。 |
| 28 | int getcontentlength() | 返回请求主体的长度(以字节为单位),并由输入流提供;如果长度未知,返回-1。 |
| 29 | int getintheader(string name) | 将指定的请求头的值作为int类型返回。 |
| 30 | int getserverport() | 返回接收到此请求的端口号。 |
http头请求示例
以下是使用httpservletrequest对象的getheadernames()方法来读取http头信息的示例。此方法返回包含与当前http请求相关联的头信息的枚举。
使用枚举,可以以标准的方式循环枚举。这里使用hasmoreelements()方法来确定何时停止,然后使用nextelement()方法来获取每个参数名称的名称。
首先打开eclipse,创建一个动态web项目:clientrequest,其项目结构如下所示 -
创建一个jsp文件:index.jsp,其代码如下所示 -
<%@ page language="java" contenttype="text/html; charset=utf-8"
pageencoding="utf-8"%>
<%@ page import="java.io.*,java.util.*"%>
<!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>客户端请求参数数据</title>
</head>
<body>
<div style="margin: auto; width: 80%;">
<h2>客户端请求头参数数据示例</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>n");
string paramvalue = request.getheader(paramname);
out.println("<td> " + paramvalue + "</td></tr>n");
}
%>
</table>
</div>
</body>
</html> 执行上面示例代码,打开浏览器访问url: http://localhost:8080/clientrequest/index.jsp ,得到以下结果 -
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!