小編給大家分享一下jsp頁(yè)面實(shí)現(xiàn)互傳數(shù)據(jù)的方法,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
HTTP是無(wú)狀態(tài)的協(xié)議。Web頁(yè)面本身無(wú)法向下一個(gè)頁(yè)面?zhèn)鬟f信息,如果需要讓下一個(gè)頁(yè)面得知該頁(yè)面中的值,除非通過(guò)服務(wù)器。因此,Web頁(yè)面保持狀態(tài)并傳遞給其它頁(yè)面,是一個(gè)重要的技術(shù)。
Web頁(yè)面之間傳遞數(shù)據(jù),是Web程序的重要功能
在此結(jié)束2種方法來(lái)完成這件事情:
1)URL傳值;
2)表單傳值;
一、URL傳值
將頁(yè)面1中的值傳給頁(yè)面
index.jsp頁(yè)面:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>My JSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> </head> <body> <% String str="318"; int number=Integer.parseInt(str); %> 該數(shù)的平方為:<%=number*number %> <hr> <a href="get_index.jsp?number=<%=number %>">到達(dá)get_index</a> </body> </html>
get_index.jsp頁(yè)面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>My JSP 'get_index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> </head> <body> <% //獲得number String str=request.getParameter("number"); int number=Integer.parseInt(str); %> 該數(shù)字的立方為:<%=number*number*number %> <hr> </body> </html>
頁(yè)面顯示結(jié)果:
優(yōu)點(diǎn):
簡(jiǎn)單性和平臺(tái)支持的多樣性(沒(méi)有瀏覽器不支持URL)。
缺點(diǎn):
1)傳輸?shù)臄?shù)據(jù)只能是字符串,對(duì)數(shù)據(jù)類型具有一定的限制;
2)傳輸數(shù)據(jù)的值會(huì)在瀏覽器地址欄里面被看到,從保密的角度講,這是不安全的。特別是秘密性要求比較嚴(yán)格的數(shù)據(jù),比如說(shuō)密碼。
二、表單傳值
方法一中通過(guò)URL傳的值會(huì)被看到,為了避免這個(gè)問(wèn)題,我們可以使用表單將頁(yè)面1中的變量傳給頁(yè)面2。
index.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>My JSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> </head> <body> <% String str="10"; int number=Integer.parseInt(str); %> 該數(shù)的平方為:<%=number*number %> <hr> <form action="get_index.jsp" method="post"> <input type="text" name="number" value="<%=number %>"> <input type="submit" value="到達(dá)get_index"> </form> </body> </html>
get_index.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>My JSP 'get_index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> </head> <body> <% //獲得number String str=request.getParameter("number"); int number=Integer.parseInt(str); %> 該數(shù)字的立方為:<%=number*number*number %> <hr> </body> </html>
頁(yè)面顯示結(jié)果:
該方法順利的進(jìn)行了值傳遞,并且無(wú)法看到傳遞的信息,在文本框中如果想要隱藏,將type=“text”改為type=“hidden”即可實(shí)現(xiàn)隱藏。
該方法的問(wèn)題:
1)和URL方法類似,該方法傳輸?shù)臄?shù)據(jù),也只能是字符串,對(duì)數(shù)據(jù)類型具有一定的限制;
2)傳輸數(shù)據(jù)的值雖然可以保證在瀏覽器地址欄里不被看到,但是在客戶端源代碼里面也會(huì)被看到,從保密的角度講,這是不安全的。對(duì)于是秘密性要求比較嚴(yán)格的數(shù)據(jù),比如說(shuō)密碼來(lái)說(shuō)還是不建議用表單來(lái)進(jìn)行傳輸。
以上是jsp頁(yè)面實(shí)現(xiàn)互傳數(shù)據(jù)的方法的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)成都網(wǎng)站設(shè)計(jì)公司行業(yè)資訊頻道!
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。
網(wǎng)頁(yè)題目:jsp頁(yè)面實(shí)現(xiàn)互傳數(shù)據(jù)的方法-創(chuàng)新互聯(lián)
文章URL:http://jinyejixie.com/article2/dejjic.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站排名、網(wǎng)頁(yè)設(shè)計(jì)公司、搜索引擎優(yōu)化、建站公司、云服務(wù)器、網(wǎng)站內(nèi)鏈
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容