(十二)会话跟踪技术之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界面也无法输出。
posted @ 2017-03-02 21:31  shyroke、  阅读(973)  评论(0编辑  收藏  举报
作者:shyroke 博客地址:http://www.cnblogs.com/shyroke/ 转载注明来源~