一、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界面也无法输出。
时间: 2024-10-12 14:57:11