关闭

Request对象请求转发

94人阅读 评论(0) 收藏 举报
分类:

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

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:204794次
    • 积分:7677
    • 等级:
    • 排名:第2884名
    • 原创:588篇
    • 转载:11篇
    • 译文:0篇
    • 评论:10条
    最新评论