关于RequestDispatcher的用法(include()和forward()的区别)

先做个例子

----------------------------------------------------------------------------------------

1.创建servlet TestDispatcher.java

package com.servlet.dispatcher;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class TestDispatcher extends HttpServlet {

public TestDispatcher(){
        super();
    }

    protected void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException{
        doPost(request, response);
    }

    protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException{
         RequestDispatcher rd ;
        rd = request.getRequestDispatcher("TestDispatcher1");
        rd.include(request, response);
//        rd.forward(request, response);//如果用forward而不用include的话,可看看他们的区别

        //String hello = (String) request.getAttribute("hello");
        //System.out.println(hello);
        rd = request.getRequestDispatcher("/dispatcher/test51.jsp");
        rd.include(request, response);


        rd = request.getRequestDispatcher("/dispatcher/test5.jsp");
        rd.include(request, response);
    }

}


2.创建Servlet TestDispatcher1.java

package com.servlet.dispatcher;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class TestDispatcher1 extends HttpServlet {

/**
* Constructor of the object.
*/
public TestDispatcher1() {
   super();
}


public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {

   doPost(request,response);
}


public void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {

   request.setAttribute("hello", "hello");
        response.getWriter().write("this is servlet2<br>");
        response.sendRedirect("http://www.csdn.net");

}

}

3.在WebRoot的dispatcher下边创建jsp页面test51.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ taglib uri="/WEB-INF/c.tld" prefix="c" %>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
    <c:out value="${hello}"></c:out>
        this is test51.jsp.
        <br>
</body>
</html>

4.在WebRoot的dispatcher下边创建jsp页面test5.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
this is test5.jsp.
</body>
</html>

5.web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

<servlet>
        <servlet-name>HelloWorld</servlet-name>
        <servlet-class>com.servlet.HelloWorld</servlet-class>
    </servlet>
<servlet>
    <servlet-name>TestDispatcher1</servlet-name>
    <servlet-class>com.servlet.dispatcher.TestDispatcher1</servlet-class>
</servlet>

<servlet>
    <servlet-name>TestDispatcher</servlet-name>
    <servlet-class>com.servlet.dispatcher.TestDispatcher</servlet-class>
</servlet>


    <servlet-mapping>
        <servlet-name>HelloWorld</servlet-name>
        <url-pattern>/servlet/helloworld</url-pattern>
    </servlet-mapping>
<servlet-mapping>
    <servlet-name>TestDispatcher1</servlet-name>
    <url-pattern>/servlet/TestDispatcher1</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>TestDispatcher</servlet-name>
    <url-pattern>/servlet/TestDispatcher</url-pattern>
</servlet-mapping>

<welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

注:1.在第3步中的页面中有<c:out value="${hello}"></c:out>代码,可知此工程中必须要引用jstl标签

       2.此例子在eclipse3.2.2+Myeclipse5.5.1 版本中测试通过(jsst采用的是1.1版本)

       3.测试结果

          在浏览器中输入http://localhost:8080/servletTest/servlet/TestDispatcher

          结果:this is servlet2
                     hello this is test51.jsp.
                     this is test5.jsp.

------------------------------------------------------------------------------------------------------------------

javax.servlet.RequestDispatcher类用于控制servlet中页面转移,有forward()和include()两个方法

从上例可以看出,当通过浏览器调用Servlet   TestDispatcher时,

        RequestDispatcher rd ;
        rd = request.getRequestDispatcher("TestDispatcher1");
        rd.include(request, response);
这一段代码,将首先通过servlet-name   "TestDispatcher1"执行servlet TestDispatcher1,但并不调用该servlet中的控制页面转移的代码response.sendRedirect("http://www.csdn.net");执行完其中的代码后然后继续执行TestDispatcher中的内容

        rd = request.getRequestDispatcher("/dispatcher/test51.jsp");
        rd.include(request, response);

将test51.jsp文件中的内容放到response中,

        rd = request.getRequestDispatcher("/dispatcher/test5.jsp");
        rd.include(request, response);

将test5.jsp文件中的内容放到response中,然后输出。

如果将

         RequestDispatcher rd ;
        rd = request.getRequestDispatcher("TestDispatcher1");
        rd.include(request, response);
改为

        RequestDispatcher rd ;
        rd = request.getRequestDispatcher("TestDispatcher1");
//      rd.include(request, response);
        rd.forward(request, response);//如果用forward而不用include的话,可看看他们的区别

就执行TestDispatcher1中的response.sendRedirect("http://www.csdn.net");代码转到http://www.csdn.net页面了。

你可能感兴趣的:(html,jsp,Web,servlet,C#)