首页 > 代码库 > (十二)会话跟踪技术之servlet通信(forward和include)

(十二)会话跟踪技术之servlet通信(forward和include)

一、servlet通信方法
技术分享
二、具体应用
  • scopeServlet.java
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out=response.getWriter();
		out.print("第yi个页面");
		RequestDispatcher dis=this.getServletContext().getRequestDispatcher("/servlet/getscope");
		dis.include(request,response);
		out.close();
	}


  •    getScopeServlet.java
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {	
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out=response.getWriter();
		out.print("第er个页面");	}

 

    ps:无论是RequestDispatcher dis=this.getServletContext().getRequestDispatcher("/servlet/getscope");
dis.include(request,response);还是request.getRequestDispatcher("getscope").forward(request,response); 都要添加forwa或者include方法,否则就不能跳转
ps: 特别注意上文中的out.close();的位置,如果它跟在out.print("第yi个页面");的后面,则结果
技术分享
因为out流已经关闭了,即使跳转到getScopeServlet.java界面也无法输出。

(十二)会话跟踪技术之servlet通信(forward和include)