YuFeng

转发代码得实现
package cn.webzhuanfa; import java.io.IOException; impo...
扫描右侧二维码阅读全文
20
2019/06

转发代码得实现

package cn.webzhuanfa;

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 Servlet1 extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //request是一个域对象.注意只对本次访问有效
        request.setAttribute("name","afeng");
        //获得一个转发器
        RequestDispatcher requestDispatcher = request.getRequestDispatcher("/s2");
        //执行转发器的操作
        requestDispatcher.forward(request, response);
    }

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







package cn.webzhuanfa;

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 Servlet2 extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String attribute = (String)request.getAttribute("name");
        System.out.println(attribute);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
Last modification:June 20th, 2019 at 10:59 am
If you think my article is useful to you, please feel free to appreciate

Comment here is closed