標籤:jsp
JSP屬性範圍,通過以下幾個測試代碼來學習request屬性的範圍
測試一(JSP動態指令方式傳參):測試內容:
<jsp:param .../>添加參數,通過<jsp:forward page="...">來實現伺服器端跳轉,以此來測試request屬性的範圍:
頁面RequestScopeDemo.jsp (添加info1參數) —> 通過<jsp:forward page="RequestScopeDemo_1.jsp">跳轉
↓
頁面RequestScopeDemo_1.jsp (添加info2參數) —> 通過<jsp:forward page="RequestScopeDemo_2.jsp">跳轉
↓
頁面RequestScopeDemo_2.jsp (request對象中取到了info1、info2兩個參數的值)
CODE:RequestScopeDemo.jsp :
<%@ page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%><html><head><title>Request屬性範圍 - Page 1</title></head><body><h1>跳轉前的頁面:page 1</h1><%//想輸入一個字串“<jsp:param ... >”不能直接輸出必須通過轉義輸出String info1="page 1 頁面中添加(<jsp:param ... />)的參數 !";request.setCharacterEncoding("utf-8");%><jsp:forward page="RequestScopeDemo_1.jsp"><jsp:param name="info1" value="<%=info1%>" /></jsp:forward></body></html>RequestScopeDemo1.jsp :
<%@ page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%><html><head><title>Request屬性範圍 - page2</title></head><body><h1>跳轉中頁面:page 2</h1><%String info2="page 2 頁面中添加(<jsp:param ... />)的參數 !";%>info參數:<%=request.getParameter("info2")%><br/><jsp:forward page="RequestScopeDemo_2.jsp"><jsp:param name="info2" value="<%=info2%>" /></jsp:forward></body></html>RequestScopeDemo2.jsp :
<%@ page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%><html><head><title>Request屬性範圍 - page3</title></head><body><h1>跳轉到的頁面:page 3</h1><%/*request對象的源頭設定了編碼,所以這裡就不需要設定啦*///request.setCharacterEncoding("utf-8");%><h2>Page1頁面中傳遞過來的info1參數:<%=request.getParameter("info1")%></h2><br/><h2>Page2頁面中傳遞過來的info2參數:<%=request.getParameter("info2")%></h2><br/><h2>request.getCharacterEncoding()的值:<%=request.getCharacterEncoding()%></h2></body></html>
測試結果:
測試二(request.setAttribute()封裝屬性方式傳參):
測試內容:
request.setAttribute()方法封裝參數,通過<jsp:forward page="...">來實現伺服器端跳轉,以此來測試request屬性的範圍:
頁面RequestScopeDemo_t.jsp (添加info1參數) —> 通過<jsp:forward page="RequestScopeDemo_t1.jsp">跳轉
↓
頁面RequestScopeDemo_t1.jsp (添加info2參數) —> 通過<jsp:forward page="RequestScopeDemo_t2.jsp">跳轉
↓
頁面RequestScopeDemo_t1.jsp (request對象中取到了info1、info2兩個參數的值)
CODE:
RequestScopeDemo_t.jsp :
<%@ page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%><html><head><title>Request屬性範圍 - Page 1</title></head><body><h1>跳轉前的頁面:page 1</h1><%request.setCharacterEncoding("utf-8");request.setAttribute("info1","Page1中添加(setAttribute)的參數");%><jsp:forward page="RequestScopeDemo_t1.jsp" /></body></html>RequestScopeDemo_t1.jsp :
<%@ page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%><html><head><title>Request屬性範圍 - page2</title></head><body><h1>跳轉中頁面:page 2</h1><%request.setAttribute("info2","Page2中添加(setAttribute)的參數");%>跳轉中的頁面,info1參數:<%=request.getAttribute("info1")%><br/><jsp:forward page="RequestScopeDemo_t2.jsp" /></body></html>RequestScopeDemo_t2.jsp :
<%@ page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%><html><head><title>Request屬性範圍 - page3</title></head><body><h1>跳轉到的頁面:page 3</h1><h2>第一個頁面,info1參數:<%=request.getAttribute("info1")%></h2><br/><h2>第二個頁面,info2參數:<%=request.getAttribute("info2")%></h2><br/><h2>request.getCharacterEncoding()的值:<%=request.getCharacterEncoding()%></h2></body></html>
測試結果:
測試三(URL地址方式傳參):測試內容:
URL地址方式添加參數,通過超連結<a href=".." ..>來實現服用戶端跳轉,以此來測試request屬性的範圍:
頁面RequestScopeDemo_1.jsp 頁中通過<a href="RequestScopeDemo_2.jsp?info1=Page1頁面的參數">連結到下一個頁面</a>跳轉
↓
頁面RequestScopeDemo_2.jsp 頁中通過<a href="RequestScopeDemo_3.jsp?info2=Page1頁面的參數">連結到下一個頁面</a>跳轉
↓
頁面RequestScopeDemo_2.jsp(request對象中取到了info2的參數值)
CODE:RequestScopeDemo_1.jsp :
<%@ page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%><html><head><title>Qequest屬性範圍 - Page 1</title></head><body><h1>跳轉前的頁面:page 1</h1><%request.setCharacterEncoding("utf-8");%><a href="RequestScopeDemo_2.jsp?info1=Page1頁面的參數">連結到下一個頁面</a></body></html>
RequestScopeDemo_2.jsp :
<%@ page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%><html><head><title>Request屬性範圍 - page2</title></head><body><h1>跳轉中頁面:page 2</h1><%request.setAttribute("info2","Page2中的參數");%>跳轉中的頁面,info1參數:<%=request.getParameter("info1")%><br/><a href="RequestScopeDemo_3.jsp?info2=Page2頁面的參數">連結到下一個頁面</a></body></html>
RequestScopeDemo_3.jsp :
<%@ page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%><html><head><title>Request屬性範圍 - page3</title></head><body><h1>跳轉到的頁面:page 3</h1><h2>第一個頁面,info1參數:<%=request.getParameter("info1")%></h2><br/><h2>第二個頁面,info2參數:<%=request.getParameter("info2")%></h2><br/><h2>request.getCharacterEncoding()的值:<%=request.getCharacterEncoding()%></h2></body></html>
測試結果:
總結:request屬性在伺服器端跳轉時request相當於是一個訊息資訊在各個頁面中傳遞,各個頁面是共用同一個request對象。
JSP中Request屬性範圍