jsp内置对象学习笔记之request

发表于: java/j2ee | 作者: darkmi | 日期: 2010/01/08 08:00
字号:

request对象
request对象封装了客户端的请求信息,服务端通过request对象可以了解到客户端的需求,然后做出响应。request对象是HttpServletRequest(接口)的实例。HttpServletRequest接口继承自ServletRequest接口,只是增加了一些HTTP相关的方法。所谓的request(在JSP中使用的)其实只是规范中的一个名称而已,它是一个对象,但并不是由SUN提供,而是由各个不同的Servlet提供商编写的,SUN只是规定这个类要实现HttpServletRequest接口,并且规定了各个方法的用途,但具体的实现则由各个提供商自己决定。

request对象的主要方法:

(1)setAttribute(String name, Object value) 和 getAttribute(String name)
前者用来在request中保存一个属性值,后者则返回name指定的属性值,如果指定的属性值不存在,则返回一个null值。这一对方法用来在不同的请求之间传递数据,而且从上一个请求到下一个请求必须是转发请求(forward),而不能是重定向请求(redirect)。API文档中已经有明确的说明,这两个方法是同RequestDispatcher联合使用的。

一个请求转发的例子:

<%@ page language="java" pageEncoding="GBK"%>
<html>
  <head>
    <title>a.jsp</title>
  </head>
  <body>
  <%
  request.setAttribute("aaa","darkmi");
  request.getRequestDispatcher("b.jsp").forward(request, response);
  %>
  </body>
</html>

页面显示:

darkmi

一个请求重定向的例子:
a.jsp:

<%
request.setAttribute("a","a");   
%>   
<form  action="b.jsp" method="post"/>   
<input type="submit" name="submit" value="add"/>   
</form>

b.jsp

<%=request.getAttribute("a")%>   

在a.jsp中点击add按钮之后跳转到b.jsp,得到的是null。

再来一个请求重定向的例子:
a.jsp:

<%   
request.setAttribute("a","a");   
%>   
<a href="b.jsp">b.jsp</a>

b.jsp

<%=request.getAttribute("a")%>   

b.jsp里得到的也是null。

(2)getParameter(String)
获得客户端传送给服务端的参数值,该参数由name指定。

示例:
a.jsp

<%@ page language="java" pageEncoding="GBK"%>
<html>
  <head>
    <title>a.jsp</title>
  </head>
  <body>
  <form action="b.jsp" method="post">
  <input type="text" name="username" />
  <input type="submit" value="提交" />
  </form>
  </body>
</html>

b.jsp

<%@ page language="java" pageEncoding="GBK"%>
<html>
  <head>
    <title>b.jsp</title>
  </head>
  <body>
  <%
  out.println(request.getParameter("username"));
  %>
  </body>
</html>

通过request.getParameter()方法从b.jsp中获取a.jsp中提交的表单数据。

注意getAttribute方法和getParameter的区别,前者是在服务端通过程序设置的,后者是由客服端传入。

(3)void setCharacterEncoding(String charEncoding)和String getCharacterEncoding()
前者用来设置请求的字符编码格式,后者返回请求的当前字符编码格式。

(4)getContextPath()和getRealPath()
前者获取相对路径,后者回去绝对路径。
http://heisetoufa.javaeye.com/blog/227640

(5)以下方法用来获取客户端请求中的相关信息:
int getContentLength() 返回请求体的长度(以字节数)。
String getContentType() 得到请求体的MIME类型。
String getProtocol() 返回请求用的协议类型及版本号。
String getScheme() 返回请求用的协议名,如:http.https及ftp等。
String getServerName() 返回接受请求的服务器主机名
int getServerPort() 返回服务器接受此请求所用的端口号
String getRemoteAddr() 返回发送此请求的客户端IP地址
String getRemoteHost() 返回发送此请求的客户端主机名
String getRealPath(String path) 返回一虚拟路径的真实路径

这些方法并不常用,可以自己写个简单的jsp测试一下,有一个大概的了解即可:

请求方式:<%=request.getMethod()%><br/> 
请求的资源:<%=request.getRequestURI()%><br/> 
请求用的协议:<%=request.getProtocol()%><br/> 
请求的文件名:<%=request.getServletPath()%><br/> 
请求的服务器的IP:<%=request.getServerName()%><br/> 
请求服务器的端口:<%=request.getServerPort()%><br/> 
客户端IP地址:<%=request.getRemoteAddr()%><br/> 
客户端主机名:<%=request.getRemoteHost()%><br/> 
协议名:<%=request.getScheme() %>

HttpServletRequest的API文档:
http://java.sun.com/j2ee/sdk_1.3/techdocs/api/javax/servlet/http/HttpServletRequest.html

ServletRequest接口的API文档:
http://java.sun.com/j2ee/sdk_1.3/techdocs/api/javax/servlet/ServletRequest.html

切换主题切换主题
返回顶部