Servlet中的请求包含

public class SrcIncludeServlet extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("include...get....11111");
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();

out.println("include1111..get...包含之前...");
//out.flush();
//out.close();
//请求包含
RequestDispatcher rd = request.getRequestDispatcher("/servlet/DestIncludeServlet");
rd.include(request, response);//包含---可理解成类似调用函数的方式。 主要是调用第2个servlet中的service(req,resp)
out.println("include1111..get...包含之后...");

}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("include...post....11111");
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");

out.println("include1111..post...包含之前...");
//请求包含
RequestDispatcher rd = request.getRequestDispatcher("/servlet/DestIncludeServlet");
rd.include(request, response);//包含
out.println("include1111..post...包含之后...");

out.println(" </BODY>");
out.println("</HTML>");
}

}

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

public class DestIncludeServlet extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("include...get....22222");
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
out.println("include2222..get页面数据输出......");
}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("include...post....22222");
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");

out.println("include2222..post页面数据输出......");

out.println(" </BODY>");
out.println("</HTML>");
}

}

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

<a href="<%=request.getContextPath()%>/servlet/srcIncludeServlet?name=Jack&age=22">请求包含时,两个servlet的访问顺序, 读取页面数据, 传参</a>
<form action="<%=request.getContextPath()%>/servlet/srcIncludeServlet" method="post">
姓名:<input type="text" name="name"/>
<input type="submit" value="提交">
</form>

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

5、请求包含(共享同一个request和response ,类似函数调用中的传参)
1)通过request实现,与转发一样是通过RequestDispatcher,只是前者是rd.forward(req,resp),后者是rd.include(req,resp);
2)在第1个servlet中out.flush(),对两个servlet的输出都没有影响。但如果在第1个servlet中out.close(),则第1个之后的输出以及第2个servlet中的输出都无效(不会输出),注意,后面的rd.include()仍然有效即第2个servlet虽然不能输出但仍会执行,且没有异常报出!
3)页面内容输出顺序:
servlet1..包含之前的输出...
servlet2.....(被包含的servlet输出)
servlet1..包含之后的输出...
4)由于该方式和转发一样是共享同一个request和response,因此有关页面参数读取和两个servlet之间的传参特性,同转发!
5)访问顺序(同转发): 第1个GET方式,第2个走的是doGet()。 第1个POST方式,第2个走的是doPost()。

6)一个小细节:被包含servlet中的所有输出信息(包括<html><body>等标记内容)都会原样输出,
//因此要注意,两个servlet之间的输出内容不要出现标记嵌套冲突! ( 如:<html><body><html><body>...</body></html></body></html> )

posted @ 2016-10-12 09:05  折腾青春  阅读(1307)  评论(0编辑  收藏  举报