请求转发 include 就是相当于能拿到 其他界面的数据,可以很好的复用。比如网页的头尾
private void test2(HttpServletRequest request, HttpServletResponse response) {
try {
response.setHeader("content-type", "text/html;charset=UTF-8");
response.setCharacterEncoding("UTF-8");
request.getRequestDispatcher("/ServletHeader").include(request, response);//引用ServletHeader
response.getOutputStream().write("This is servletDemo3".getBytes());
request.getRequestDispatcher("/ServletFooter").include(request, response);//引用ServletFooter
} catch (Exception e) {
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setHeader("content-type", "text/html;charset=UTF-8");
response.setCharacterEncoding("UTF-8");
response.getOutputStream().write("This is footer".getBytes());
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setHeader("content-type", "text/html;charset=UTF-8");
response.setCharacterEncoding("UTF-8");
response.getOutputStream().write("This is header".getBytes());
}