只是在书上看到了RequestDispatcher对象的Include方法可以将多个单功能的文件例如.jsp文件整合成一个Servlet文件

今天上网找到一个很好的实例来说明这一点。很感谢此作者!^_^

 

[转载]RequestDispatcher.include(String str)用法。_htmlpublic

  class  TestDispatcher  extends  javax.servlet.http.HttpServlet  implements

[转载]RequestDispatcher.include(String str)用法。_java_02        javax.servlet.Servlet ...

{

[转载]RequestDispatcher.include(String str)用法。_java_03    public TestDispatcher() ...{

[转载]RequestDispatcher.include(String str)用法。_java_04        super();

[转载]RequestDispatcher.include(String str)用法。_html_05    }

[转载]RequestDispatcher.include(String str)用法。_java_04

[转载]RequestDispatcher.include(String str)用法。_java_04    protected void doGet(HttpServletRequest request,

[转载]RequestDispatcher.include(String str)用法。_java_03            HttpServletResponse response) throws ServletException, IOException ...{

[转载]RequestDispatcher.include(String str)用法。_java_04        doPost(request, response);

[转载]RequestDispatcher.include(String str)用法。_html_05    }

[转载]RequestDispatcher.include(String str)用法。_java_04

[转载]RequestDispatcher.include(String str)用法。_java_04    protected void doPost(HttpServletRequest request,

[转载]RequestDispatcher.include(String str)用法。_java_03            HttpServletResponse response) throws ServletException, IOException ...{

[转载]RequestDispatcher.include(String str)用法。_java_04            RequestDispatcher rd ;

[转载]RequestDispatcher.include(String str)用法。_java_04        rd = request.getRequestDispatcher("TestDispatcher1");

[转载]RequestDispatcher.include(String str)用法。_java_04        rd.include(request, response);

[转载]RequestDispatcher.include(String str)用法。_java_04

[转载]RequestDispatcher.include(String str)用法。_java_04        //String hello = (String) request.getAttribute("hello");

[转载]RequestDispatcher.include(String str)用法。_java_04        //System.out.println(hello);

[转载]RequestDispatcher.include(String str)用法。_java_04        rd = request.getRequestDispatcher("/test/test51.jsp");

[转载]RequestDispatcher.include(String str)用法。_java_04        rd.include(request, response);

[转载]RequestDispatcher.include(String str)用法。_java_04

[转载]RequestDispatcher.include(String str)用法。_java_04        rd = request.getRequestDispatcher("/test/test5.jsp");

[转载]RequestDispatcher.include(String str)用法。_java_04        rd.include(request, response);

[转载]RequestDispatcher.include(String str)用法。_html_05    }

[转载]RequestDispatcher.include(String str)用法。_html_26}

 

[转载]RequestDispatcher.include(String str)用法。_htmlpublic

  class  TestDispatcher1  extends  javax.servlet.http.HttpServlet  implements

[转载]RequestDispatcher.include(String str)用法。_java_02        javax.servlet.Servlet ...

{

[转载]RequestDispatcher.include(String str)用法。_java_03    public TestDispatcher1() ...{

[转载]RequestDispatcher.include(String str)用法。_java_04        super();

[转载]RequestDispatcher.include(String str)用法。_html_05    }

[转载]RequestDispatcher.include(String str)用法。_java_04

[转载]RequestDispatcher.include(String str)用法。_java_04    protected void doGet(HttpServletRequest request,

[转载]RequestDispatcher.include(String str)用法。_java_03            HttpServletResponse response) throws ServletException, IOException ...{

[转载]RequestDispatcher.include(String str)用法。_java_04        doPost(request, response);

[转载]RequestDispatcher.include(String str)用法。_html_05    }

[转载]RequestDispatcher.include(String str)用法。_java_04

[转载]RequestDispatcher.include(String str)用法。_java_04    protected void doPost(HttpServletRequest request,

[转载]RequestDispatcher.include(String str)用法。_java_03            HttpServletResponse response) throws ServletException, IOException ...{

[转载]RequestDispatcher.include(String str)用法。_java_04            request.setAttribute("hello", "hello");

[转载]RequestDispatcher.include(String str)用法。_java_04            response.getWriter().write("this is servlet2<br>");

[转载]RequestDispatcher.include(String str)用法。_java_04            response.sendRedirect();

[转载]RequestDispatcher.include(String str)用法。_html_05    }

[转载]RequestDispatcher.include(String str)用法。_html_26}

 <% ...@ 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 >
< br >
this is test51.jsp.
< br >
</ body >
</ html >

 

 

 <% ...@ 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 >

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

总结:servlet1中include另外一个servlet2,会计算servlet2,最终转向servlet1决定,servlet2中的request和response对象中存储的数据被和并到servlet1的request和response对象中。

servlet中include一个jsp页,相当于:jsp中<%@ include file="XXX.jsp" %>

都表示要跳转到其他资源,不同的是,如果使用forward跳转则后面的response输出则不会执行,而用include
来跳转,则include的servlet执行完后,再返回到原来的servlet执行response的输出(如果有)。如:
servlet A
RequestDispatcher disp = request.getRequestDispatcher("B");
disp.forward(request, response);
System.out.println("servlet A completed");
PrintWriter pw = response.getWriter();
pw.println("servlet A");

servlet B
PrintWriter pw = response.getWriter();
pw.println("servlet B");

输出结果:
控制台:servlet A completed
页面:servlet B
如果将forward换成include的话,则结果为:
控制台:servlet A completed
页面:servlet B servlet A
PS:如果在servlet B里吧pw给close掉了的话,那servlet A 这里就无法输出了,则结果就和第一个一样。

版权声明:本文为博主原创文章,未经博主允许不得转载。