Web_Servlet和jsp页面数据交互,通过请求转发在jsp中显示数据

1.Servlet页面代码

/*
    实现jsp页面和sevlet页面的信息交互
 */
@WebServlet(urlPatterns = "/aa")
public class JspService extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

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

        //以键值对存储数据

        request.setAttribute("name","张三丰");

        //进行页面跳转,把信息传过去
        request.getRequestDispatcher("/demojsp.jsp").forward(request,response);

    }
}

2.Jsp页面

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <div>
        <font color="#808080"><%=request.getAttribute("name")%></font>
    </div>
</body>
</html>
原文地址:https://www.cnblogs.com/LVowe/p/13202083.html