Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2502742
  • 博文数量: 709
  • 博客积分: 12251
  • 博客等级: 上将
  • 技术积分: 7905
  • 用 户 组: 普通用户
  • 注册时间: 2005-07-17 00:00
个人简介

实现有价值的IT服务

文章存档

2012年(7)

2011年(147)

2009年(3)

2008年(5)

2007年(74)

2006年(431)

2005年(42)

分类: Java

2006-10-12 22:32:21

javax.servlet
Interface RequestDispatcher


public interface RequestDispatcher

Defines an object that receives requests from the client and sends them to any resource (such as a servlet, HTML file, or JSP file) on the server. The servlet container creates the RequestDispatcher object, which is used as a wrapper around a server resource located at a particular path or given by a particular name. 定义了一个对象用来接收客户端的请求,将它们发送给服务器上的任何资源(比如servlet,HTML文件,或者JSP文件)。 servlet容器创建了RequestDispatcher对象,用于包装以特定路径或特定名称定位的服务器资源。

This interface is intended to wrap servlets, but a servlet container can create RequestDispatcher objects to wrap any type of resource. 该接口目的是包装servlet,但是servlet容器创建的RequestDispatcher可以包装任何类型的资源。

Version:
$Version$
Author:
Various
See Also:
, ,

Method Summary
 void ( request,  response)
          Forwards a request from a servlet to another resource (servlet, JSP file, or HTML file) on the server. 从servlet向服务器上其他资源(servlet,JSP文件或者HTML文件)转发请求。
 void ( request,  response)
          Includes the content of a resource (servlet, JSP page, HTML file) in the response. 包含响应中的资源(servlet,JSP文件,HTML文件)内容。
 

Method Detail

forward

public void forward( request,
                     response)
             throws ,
                    
Forwards a request from a servlet to another resource (servlet, JSP file, or HTML file) on the server. This method allows one servlet to do preliminary processing of a request and another resource to generate the response. 从servlet向服务器上其他资源(servlet,JSP文件或者HTML文件)转发请求。该方法允许请求和其他资源生成响应之前做一些预处理。

For a RequestDispatcher obtained via getRequestDispatcher(), the ServletRequest object has its path elements and parameters adjusted to match the path of the target resource. 对于通过getRequestDispatcher()获得的RequestDispatcher,ServletRequest对象可以调整路径组成和参数来匹配目标资源的路径。

forward should be called before the response has been committed to the client (before response body output has been flushed). If the response already has been committed, this method throws an IllegalStateException. Uncommitted output in the response buffer is automatically cleared before the forward. forward应当在响应提交给客户端之前调用(响应体输出被刷新前)。如果响应已经被提交,该方法抛出IllegalStateException。响应缓存中未提交的输出在转发之前自动清空。

The request and response parameters must be either the same objects as were passed to the calling servlet's service method or be subclasses of the or classes that wrap them. 传递给调用servlet的service方法的请求和响应参数可以是相同的对象,也可以是 对它们进行包装的ServletRequestWrapper或ServletResponseWrapper类的子类。

Parameters:
request - a object that represents the request the client makes of the servlet ServletRequest对象,表示客户端向servlet发出的请求
response - a object that represents the response the servlet returns to the client ServletResponse对象,表示servlet返回给客户端的响应
Throws:
- if the target resource throws this exception 如果目标资源抛出该异常时抛出
- if the target resource throws this exception 如果目标资源抛出该异常时抛出
- if the response was already committed 如果响应已经提交则抛出

include

public void include( request,
                     response)
             throws ,
                    
Includes the content of a resource (servlet, JSP page, HTML file) in the response. In essence, this method enables programmatic server-side includes. 包含响应中的资源(servlet,JSP文件,HTML文件)内容。本质上,该方法使服务器端包含成为可能。

The object has its path elements and parameters remain unchanged from the caller's. The included servlet cannot change the response status code or set headers; any attempt to make a change is ignored. 来自调用者的ServletResponse对象的路径组成和参数保持不变。包含的servlet 不能改变响应状态代码或设置头;任何变更的企图都会被忽略。

The request and response parameters must be either the same objects as were passed to the calling servlet's service method or be subclasses of the or classes that wrap them. 传递给调用servlet的service方法的请求和响应参数可以是相同的对象,也可以是 对它们进行包装的ServletRequestWrapper或ServletResponseWrapper类的子类。

Parameters:
request - a object that contains the client's request ServletRequest对象,表示客户端请求
response - a object that contains the servlet's response ServletResponse对象,表示servlet响应
Throws:
- if the included resource throws this exception 如果包含的资源抛出该异常时抛出
- if the included resource throws this exception 如果包含的资源抛出该异常时抛出

=========================================

RequestDispatcher

forward

1. 如果 Forward URL 中有参数, 并且与 request 对象中现有的参数同名, 则 getParameter 方法返回 Forward URL中的参数
2. 在 Forward 之前, 容器应读取 POST Form Data 中的参数
3. 如果 Forward URL 中带有 jsessionid 参数, 则容器将其当做 URL 的一部分, 而不做 SessionId
4. 在 forward 方法调用之前, 容器先清除输出缓存, 如果数据已发送到浏览器, 则抛出 IllegalStateException
5. 在 forward 方法返回之前, 容器应将所有数据发送到浏览器, 并关闭输出流, 这就意味着 forward 方法调用后, 所有使用输出流输出的数据都将忽略
6. 在 forward 之前, 容器应根据 Forward URL 重设 request 以下属性: ServletPath, RequestURI, QueryString, PathInfo. RequestURL 不变, 如果是用 getNameDispatcher 得到的, 就不需要重设
7. Forward 请求时, 应当处理 welcome files
8. Forward 时, 不做 Filting
9. request 和 response 参数可以是容器的实现类, 也可以 ServletRequestWrapper 和 ServletResponseWrapper 或其子类

include

1. 如果 Include URL 中有参数, 并且与 request 对象中现有的参数同名, 则 getParameter 方法返回 Include URL中的参数
2. 在 Include 之前, 容器应读取 POST Form Data 中的参数
3. 如果 Include URL 中带有 jsessionid 参数, 则容器将其当做 URL 的一部分, 而不做 SessionId
4. 在被包含的 Servlet 中, 只通过输出流输出内容, 不能设响应头, 所有的设置都被忽略, 但不抛异常
5. 在 include 之前, 容器不重设 request 各种属性, 但将被包含 ContextPath, ServletPath, PathInfo, QueryString, RequestURI 放到 request 的 attribute 中 (如果是使用 getNameDispather 则不设)

 javax.servlet.include.context_path = ContextPath
 javax.servlet.include.servlet_path = ServletPath
 javax.servlet.include.path_info = PathInfo
 javax.servlet.include.query_string = QueryString
 javax.servlet.include.request_uri = RequestURI

6. Include 时, 不做 Filting
7. Include 请求时, 应当不处理 welcome files
8. request 和 response 参数可以是容器的实现类, 也可以 ServletRequestWrapper 和 ServletResponseWrapper 或其子类

通过 ServletContext 得到的 RequestDispatcher, 其 URL 是相对于 ContextPath 的
通过 ServletRequest 得到的 RequestDispatcher, 其 URL 是相对于最初请求的 ServletPath (如果是一串的 forward , 最好处理请求的 Servlet 的 Path)

调用 getNameDispatcher 如果 Servlet 不存在, 则返回 null

==========================
1.首先建立一个html页收集输入,傻子都会,传递参数为color
2.然后Servlet 收集参数,并以参数通过JavaBean建立一个实例
Bean object =new Bean(color);
3.接着通过JavaBean的get方法返回color, Servlet 进行判断.//equals() method.
object.getColor();
4.servlet 判断后转到相应的jsp页面
String addr=...
核心方法:
reqest.setAttribute("Instance", object);
RequestDispatcher dispatcher=request.getRequestDispatcher(addr);
dispatcher .forward(request, response);
5.jsp 页面根据前面建立的实例读取相应的String.


这个程序开始是按书上抄的,后来完全是自己写的
这下面的代码还不是很理解,到底建立了几个对象?那个是实例?特别是setAttribute("Red", color);
里是用前面的替换后边的吗?
String tempColor=request.getParameter("color");
 ColorBean color=new ColorBean(tempColor);
request.setAttribute("Red", color);
 RequestDispatcher dispatcher=request.getRequestDispatcher(addr);
 dispatcher.forward(request, response);
现在可以实现mvc架构了,但是,其中对象的产生过程还是不理解,对象啊,对象......
下边是源代码:
其中
Input.jsp 为收集颜色(string)页面
WantColor.java 为servlet
ColorBean.java 为JavaBean
其它的是三个jsp 页面用来连接javaBean 显示颜色
***********
//input.jsp
<%@ page contentType="text/html;charset=GB2312"%>

 
   
    请输入你想要的颜色
 
 
 


 
 
 

 

**************
//wantColors.java
package moonsoft.test.j2ee.wantColors;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.PrintWriter;
import java.io.IOException;
public class WantColors extends HttpServlet
{
  private static final String CONTENT_TYPE = "text/html; charset=Big5";
  public void init(ServletConfig config) throws ServletException
  {
    super.init(config);
  }
  public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
  {
    String tempColor=request.getParameter("color");
   ColorBean color=new ColorBean(tempColor);
    if((color.getColor()==null)&&(color.getColor()).equals(""))
    {
      addr="../input.jsp";
      response.sendRedirect("");
    }
    else if(color.getColor().equals("red"))
    {
      request.setAttribute("Red", color);
      addr="../red.jsp";
    }
    else if(color.getColor().equals("green"))
    {
      request.setAttribute("Green",color );
      addr="../green.jsp";
    }
    else
    {
      request.setAttribute("Blue",color );
      addr="../blue.jsp";
    }
    RequestDispatcher dispatcher=request.getRequestDispatcher(addr);
    dispatcher.forward(request, response);
 
    /*response.setContentType(CONTENT_TYPE);
    PrintWriter out = response.getWriter();
    out.println("");
    out.println("WantColors");
    out.println("");
    out.println("

The servlet has received a GET. This is the reply.

");
    out.println("");
    out.close();*/
  }
  public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
  {
    doGet(request, response);
    /*response.setContentType(CONTENT_TYPE);
    PrintWriter out = response.getWriter();
    out.println("");
    out.println("WantColors");
    out.println("");
    out.println("

The servlet has received a POST. This is the reply.

");
    out.println("");
    out.close();*/
  }
  private String addr;
}
***********************
//ColorBean.java
package moonsoft.test.j2ee.wantColors;
public class ColorBean
{
  public ColorBean(String color)
  {
  this.color=color;
  }
  public String getString()
  {
    String s="The color is  :\t";
    return s+color;  
  }
  public String  getColor()
  {
    return color;
  }
  public void setColor(String color)
  {
    this.color=color;
  }
  private String color;
}
**************
red.jsp
<%@ page contentType="text/html;charset=Big5"%>

 
   
    red
 
 
 
 
   
 

 

那两个绿色和蓝色的copy 一下,然后改一下值就好了

阅读(5385) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~