日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

JSP中Request属性范围

發布時間:2023/12/1 javascript 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 JSP中Request属性范围 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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属性范围的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。