(十二)會話跟蹤技術之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界面也無法輸出。

注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
粤ICP备14056181号  © 2014-2020 ITdaan.com