请求包含:
1)通过request实现,与转发一样是通过RequestDispatcher,只是前者是rd.forward(req,resp),后者是rd.include(req,resp); 包含之后可以再包含其它servlet即1个servlet可同时包含多个servlet。
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()。
package cn.hncu.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class IncludeOneServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
System.out.println("IncludeOneServlet--Get...");
//请求包含--也是用到request.getRequestDispatcher()分发器
//请求包含这种方式--共享request和response
PrintWriter out=response.getWriter();
out.println("请求包含之前的输出。。");
out.flush(); //3.刷缓存后,后面的response的out流输出都是有效的.
// out.close(); //4.关流后,关流之后的out输出无效,关流之前的输出有效---也没有异常抛出,异常在内部处理
String name=request.getParameter("name");
String age=request.getParameter("age");
response.getWriter().println("IncludeOneServlet:"+name+","+age+"<br>");
//与转发不同的是
request.getRequestDispatcher("/IncludeTwoServlet").include(request, response);
//1.包含一次之后,还可以包含另外一个(可以包含多个)
request.getRequestDispatcher("/aa").include(request, response);
//2.请求包含执行之后,一定会回来继续执行
out.println("请求包含之后的输出。。");
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("IncludeOneServlet--Post...");
//请求包含--也是用到request.getRequestDispatcher()分发器
String name=request.getParameter("name");
String age=request.getParameter("age");
response.getWriter().println("IncludeOneServlet:"+name+","+age+"<br>");
request.getRequestDispatcher("/IncludeTwoServlet").include(request, response);
}
}
package cn.hncu.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class IncludeTwoServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("IncludeTwoServlet--Get...");
response.getWriter().println("请求包含的输出");
//请求包含这种方式--共享request和response
String name=request.getParameter("name");
String age=request.getParameter("age");
response.getWriter().println("IncludeTwoServlet"+name+","+age+"<br>");
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("IncludeTwoServlet--Post...");
response.getWriter().println("请求包含的输出");
String name=request.getParameter("name");
String age=request.getParameter("age");
response.getWriter().println("IncludeTwoServlet"+name+","+age+"<br>");
}
}