詳解JSP頁面?zhèn)髦?/h1>
這周在調(diào)支付寶的接口。期間需要把我方程序處理后的參數(shù)(交易金額)按照規(guī)定的格式傳遞給支付寶的接口。因為中途要設(shè)計到我方程序?qū)σ恍?shù)據(jù)的處理,所以并不方便直接傳值過去。思來想去,決定先把我方的數(shù)據(jù)提交給webwork的Action進(jìn)行處理,也就是對數(shù)據(jù)庫進(jìn)行操作;然后把交易金額以及支付寶接口需要的其他參數(shù)一并傳遞給一個JSP頁面,并讓這個JSP頁面在把a(bǔ)ction直接指向支付寶的網(wǎng)關(guān)接口,注意:中間過程中這個JSP頁面時不顯示出來的。為此,做了如下測試:建立兩個JSP頁面?zhèn)髦?tes1.jsp和test2.jsp。代碼如下:
- <%@pagelanguage="java"import="java.util.*"pageEncoding="GBK"%>
- <%
- Stringpath=request.getContextPath();
- StringbasePath=request.getScheme()+"://"+request.getServerName()
+":"+request.getServerPort()+path+"/";- %>
- >
- <html>
- <head>
- <basehrefbasehref="<%=basePath%>">
- <title>MyJSP'test1.jsp'startingpage< SPAN>title>
- <metahttp-equivmetahttp-equiv="pragma"content="no-cache">
- <metahttp-equivmetahttp-equiv="cache-control"content="no-cache">
- <metahttp-equivmetahttp-equiv="expires"content="0">
- <metahttp-equivmetahttp-equiv="keywords"content="keyword1,keyword2,keyword3">
- <metahttp-equivmetahttp-equiv="description"content="Thisismypage">
- <metahttp_equivmetahttp_equiv="refresh"content="5">
- <scriptlanguagescriptlanguage="javascript"type="text/javascript">
- functionexecute(){
- varobj=document.getElementById("name");
- document.form1.action="alipay/test2.jsp?param="+obj.value;
- document.form1.submit();
- }
- < SPAN>script>
- < SPAN>head>
- <bodyonloadbodyonload="execute();">
- <formnameformname="form1"method="post">
- <table>
- <tr>
- <td>
- 測試JSP頁面?zhèn)髦?/SPAN><inputtypeinputtype="text"id="username"value="luodada">
- < SPAN>td>
- < SPAN>tr>
- < SPAN>table>
- < SPAN>form>
- < SPAN>body>
- < SPAN>html>
tset2.jsp的代碼如下:
- <%@pagelanguage="java"import="java.util.*"pageEncoding="GBK"%>
- <%
- Stringpath=request.getContextPath();
- StringbasePath=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
- %>
- >
- <html>
- <head>
- <basehrefbasehref="<%=basePath%>">
- <title>MyJSP'test2.jsp'startingpage< SPAN>title>
- <metahttp-equivmetahttp-equiv="pragma"content="no-cache">
- <metahttp-equivmetahttp-equiv="cache-control"content="no-cache">
- <metahttp-equivmetahttp-equiv="expires"content="0">
- <metahttp-equivmetahttp-equiv="keywords"content="keyword1,keyword2,keyword3">
- <metahttp-equivmetahttp-equiv="description"content="Thisismypage">
- < SPAN>head>
- <body>
- <%
- Stringvalue=request.getParameter("param");
- out.print("從test1.jsp傳遞過來的值是"+value);
- %>
- < SPAN>body>
- < SPAN>html>
具體思路如下:
在JSP頁面?zhèn)髦祎est1.jsp中,通過JavaScript把文本框中的值獲取出來,,使test1.jsp在加載進(jìn)來的時候馬上執(zhí)行頁面跳轉(zhuǎn);
在JSP頁面?zhèn)髦祎est2.jsp中通過request.getParameter("參數(shù)名稱");來獲取test1.jsp傳遞過來的值即可。
【編輯推薦】