jsp Request 请求转发,共享数据
<%-- Created by IntelliJ IDEA. User: 13634 Date: 2022/1/122 Time: 21:52 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %>$Title$ $END$
package request; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; @WebServlet("/RequestDemo8") public class RequestDemo8 extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // String username = req.getParameter("username"); // System.out.println("get"+username); this.doPost(req,resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("demo8...."); // RequestDispatcher requestDispatcher = req.getRequestDispatcher("/RequestDemo88"); // requestDispatcher.forward(req,resp); //存储数据到request域中 req.setAttribute("msg","hello"); req.getRequestDispatcher("/RequestDemo88").forward(req,resp); } }
package request; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; @WebServlet("/RequestDemo88") public class RequestDemo88 extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // String username = req.getParameter("username"); // System.out.println("get"+username); this.doPost(req,resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //获取数据 Object msg = req.getAttribute("msg"); System.out.println(msg); System.out.println("demo88....."); } }
结果:
刷新一下:
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)