RequestDisapacher接口

释放双眼,带上耳机,听听看~!

概述

在开发中访问到一个web资源后,需要服务器跳转到另一个资源去处理请求,可以通过sendRedirect重定向方法来实现,也可RequestDispatcher转发对象的forward方法来实现

得到转发对象:RequestDispatcher forwardObj = request.getRequestDisapatcher(“另外一个资源的路径”)

常用的方法:

  1. 实现请求转发:forwardObj.forward(request,response)
  2. 实现请求集合:forwardObj》include(request,response)

请求转发

这里要注意重定向与请求转发的区别

重定向是请求到第一台服务器,立马反应给浏览器,告诉浏览器需要访问另外一台服务器,然后服务器再自发访问

请求转发是请求到第一台服务器,由服务器主动进行转发访问,然后由第二台服务器将数据反应回来

package p01;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Forward_2 extends HttpServlet {
    private static final long serialVersionUID = 1L;
    public Forward_2() {

    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        //转发是不用写文件路径的,直接写文件名
        RequestDispatcher req = request.getRequestDispatcher("/fordward_re");
        response.getWriter().print("转发前……");
        request.setAttribute("name", "LL");
        req.forward(request, response);
        //req.include(request, response);
        response.getWriter().print("转发后……");



    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

}
package p01;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class fordward_re
 */
public class fordward_re extends HttpServlet {
    private static final long serialVersionUID = 1L;
   
    /**
     * @see HttpServlet#HttpServlet()
     */
    public fordward_re() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        Object o = request.getAttribute("name");
        String name = (String) o;
        response.getWriter().print(name);


    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}

结果:只返回转发后页面的内容

## 请求包含

结果将返回原页面+转发后页面的所有输出内容

package p01;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Forward_2 extends HttpServlet {
    private static final long serialVersionUID = 1L;
    public Forward_2() {

    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        //转发是不用写文件路径的,直接写文件名
        RequestDispatcher req = request.getRequestDispatcher("/fordward_re");
        response.getWriter().print("转发前……");
        request.setAttribute("name", "LL");
        //req.forward(request, response);
        req.include(request, response);
        response.getWriter().print("转发后……");



    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

}
package p01;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class fordward_re
 */
public class fordward_re extends HttpServlet {
    private static final long serialVersionUID = 1L;
   
    /**
     * @see HttpServlet#HttpServlet()
     */
    public fordward_re() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        Object o = request.getAttribute("name");
        String name = (String) o;
        response.getWriter().print(name);


    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}

人已赞赏
知识分享

随机密码生成器

2020-9-19 22:34:00

知识分享

常见的OS体系

2020-9-19 22:38:00

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索