Request对象请求转发

RequestDemo7.java文件:

package com.neu;

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;
/*Request实现请求转发*/
//转发:源
//源和目标:共享请求范围中的数据
public class RequestDemo7 extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setAttribute("p", "ppp");
        RequestDispatcher rd = getServletContext().getRequestDispatcher("/servlet/RequestDemo8");
        //RequestDispatcher rd = request.getRequestDispatcher("/servlet/RequestDemo8");
        rd.forward(request, response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        doGet(request, response);
    }

}

RequestDemo8.java文件:
package com.neu;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//转发:目标
public class RequestDemo8 extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String s = (String) request.getAttribute("p");
        response.getWriter().write(s);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        doGet(request, response);
    }

}

运行:http://localhost:8080/Request/servlet/RequestDemo7
结果:浏览器中输出ppp

阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wangyanming123/article/details/52373083
个人分类: request
上一篇Request对象中请求参数中文编码问题
下一篇Request对象中的文件包含
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭