• jsp中 RequestDispatcher接口的两种跳转:forward()和include()


    在web程序中,经常是由多个Servlet来完成请求.RequestDispatcher接口就是为了多个Servlet之间的调整而实现的.该接口可以用httpSerbletRequest的getRequestDispatcher()方法取得.调用时指定跳转的URL地址即可完成跳转动作.RequestDispatcher接口有两种方法实现跳转

    一.include方法

    本示例不需要前台页面,直接通过地址访问.

    IncludeMethod.java代码

     1 package com.gys;
     2 
     3 import java.io.IOException;
     4 import java.io.PrintWriter;
     5 
     6 import javax.servlet.RequestDispatcher;
     7 import javax.servlet.ServletException;
     8 import javax.servlet.annotation.WebServlet;
     9 import javax.servlet.http.HttpServlet;
    10 import javax.servlet.http.HttpServletRequest;
    11 import javax.servlet.http.HttpServletResponse;
    12 
    13 @WebServlet(
    14         name="demo",
    15         urlPatterns={"/include"}
    16     )
    17 public class IncludeMethod extends HttpServlet{
    18     @Override
    19     protected void doPost(HttpServletRequest req, HttpServletResponse resp)
    20             throws ServletException, IOException {
    21         doGet(req, resp);
    22     }
    23     @Override
    24     protected void doGet(HttpServletRequest request, HttpServletResponse response)
    25             throws ServletException, IOException {
    26         response.setContentType("text/html;charset=utf-8");
    27         PrintWriter out=response.getWriter();
    28         out.println("<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">");
    29         out.println("<HTML>");
    30         out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
    31         out.println("  <BODY>");
    32         out.print(" <h1>第一个servlet</h1>");
    33         RequestDispatcher dispatcher=request.getRequestDispatcher("/includeSecond");
    34         dispatcher.include(request, response);
    35         out.println("  </BODY>");
    36         out.println("</HTML>");
    37         out.flush();
    38         out.close();
    39     }
    40 }

    IncludeMethod2.java

     1 package com.gys;
     2 
     3 import java.io.IOException;
     4 import java.io.PrintWriter;
     5 
     6 import javax.servlet.ServletException;
     7 import javax.servlet.annotation.WebServlet;
     8 import javax.servlet.http.HttpServlet;
     9 import javax.servlet.http.HttpServletRequest;
    10 import javax.servlet.http.HttpServletResponse;
    11 
    12 
    13 @WebServlet(
    14         name="include2",
    15         urlPatterns={"/includeSecond"}
    16         )
    17 public class IncludeMethod2 extends HttpServlet{
    18     @Override
    19     protected void doPost(HttpServletRequest req, HttpServletResponse resp)
    20             throws ServletException, IOException {
    21         doGet(req, resp);
    22     }
    23     @Override
    24     protected void doGet(HttpServletRequest request, HttpServletResponse response)
    25             throws ServletException, IOException {
    26         response.setContentType("text/html;charset=utf-8");
    27         PrintWriter out=response.getWriter();
    28         out.println("这是第二个servlet(被包涵在第一个servlet里面)");
    29     }
    30 }

    测试结果:

    二.forward()

    forwardMethod.java代码

     1 package com.gys2;
     2 
     3 import java.io.IOException;
     4 
     5 import javax.servlet.ServletException;
     6 import javax.servlet.annotation.WebServlet;
     7 import javax.servlet.http.HttpServlet;
     8 import javax.servlet.http.HttpServletRequest;
     9 import javax.servlet.http.HttpServletResponse;
    10 
    11 @WebServlet(
    12         name="forwardMethod",
    13         urlPatterns={"/test"}
    14         )
    15 
    16 public class forwardMethod extends HttpServlet {
    17     @Override
    18     protected void doPost(HttpServletRequest req, HttpServletResponse resp)
    19             throws ServletException, IOException {
    20         doGet(req, resp);
    21     }
    22     @Override
    23     protected void doGet(HttpServletRequest request, HttpServletResponse response)
    24             throws ServletException, IOException {
    25         request.getRequestDispatcher("/test.html").forward(request, response);
    26     }
    27 }

    运行结果是这个样子的:

  • 相关阅读:
    Jquery 改变样式
    2017年04月06日 开启博客之路
    SVN-简要说明
    wp8 入门到精通 高仿微信发信息 键盘不消失
    wp8 入门到精通 仿QQPivot 提示数量
    wp8 入门到精通 虚拟标示符 设备ID
    wp8 入门到精通 测量代码执行时间
    wp8 入门到精通 聊天控件
    wp8 入门到精通 抓包
    wp8 入门到精通 LINQ to SQL
  • 原文地址:https://www.cnblogs.com/guoyansi19900907/p/4392392.html
Copyright © 2020-2023  润新知