1. 程式人生 > 實用技巧 >Web_Servlet和jsp頁面資料互動,通過請求轉發在jsp中顯示資料

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>