事件驅(qū)動(dòng)的Web:JSP與JavaScript的融合
我們?cè)賮?lái)說(shuō)一說(shuō)JSP和JavaScript的搭配使用和相互訪問(wèn)。
也許你用過(guò)Delphi的WebBroker開(kāi)發(fā)過(guò)Web Application當(dāng)你每做的一件事(提交)就是一個(gè)WebAction,說(shuō)白了,就是一個(gè)函數(shù)。而這個(gè)函數(shù)在ASP、JSP條件下就成了一個(gè)頁(yè)面。不過(guò)你也應(yīng)該把他們理解成函數(shù),一個(gè)返回字符串的函數(shù)。而這個(gè)字符串,就是你在瀏覽器利用查看源文件菜單命令所看到的結(jié)果。這樣,JavaScript調(diào)用Java變量的問(wèn)題就迎刃而解了。我們的JSP返回的字符串有JavaScript代碼,而這些代碼是由瀏覽器解釋執(zhí)行的。由于JavaScript代碼的生成都可以由JSP動(dòng)態(tài)生成,所以,JavaScript中可以包含JSP代碼的運(yùn)行結(jié)果。這樣就實(shí)現(xiàn)了JavaScript對(duì)JSP變量的調(diào)用。
下面我們?cè)賮?lái)看一看,JavaScript對(duì)JSP方法和JSP對(duì)JavaScript的調(diào)用。這兩個(gè)調(diào)用都有一個(gè)共同點(diǎn),那就是需要對(duì)頁(yè)面進(jìn)行提交,在用Hidden設(shè)置幾個(gè)隱藏參數(shù)。JSP根據(jù)獲得的參數(shù)再進(jìn)行相應(yīng)的處理,就可以了。也許有的人已經(jīng)不太明白了,那我們還是用一個(gè)程序來(lái)說(shuō)明問(wèn)題吧!
程序二:一個(gè)服務(wù)器獲得客戶端信息的Web頁(yè)面。他獲得了客戶端的一些屏幕設(shè)置,并把它們記錄在數(shù)據(jù)庫(kù)里。
GetClientInfo.jsp
<%@ page="" contenttype="text/html; charset=utf-8" language="java">
<%
??? if(request.getParameter("isHaveData") == null){ %>
???????<OBJECT id=objMSAgent
classid=CLSID:D45FD31B-5C6E-11D1-9EC1-00C04FD7081F
?width="32" height="32">
your infomation:
??????
Screen Width: |
???????????
Screen Height: |
???????????
Color Depth: |
???????????
Browse Name: |
???????????
Browse Version: |
???????????
Client area Width: |
???????????
Client area Height: |
??????? <% String strDSN =
"jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ="
?+ application.getRealPath("http://WEB-INF//msg.mdb");
??????? Connection cnct = null;
??????? Statement stmt = null;
??????? Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
??????? cnct = DriverManager.getConnection(strDSN,"","");
??????? stmt = cnct.createStatement(
ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE);
??????? java.text.DateFormat dfNow =
java.text.DateFormat.getDateTimeInstance(
?????????????? ???java.text.DateFormat.MEDIUM,
java.text.DateFormat.MEDIUM);
??????? String strDate = dfNow.format(new java.util.Date());
??????? String sInsertSQL = "INSERT INTO ClientMsg VALUES(" +
??????????????? "/'" + request.getRemoteAddr() + "/'," +
??????????????? "/'" + strDate +"/'," +
??????????????? "/'" + request.getParameter("iScreenWidth") + "/'," +
??????????????? "/'" + request.getParameter("iScreenHeight") + "/'," +
??????????????? "/'" + request.getParameter("iColorDepth") + "/'," +
??????????????? "/'" + request.getParameter("sBrowseName") + "/'," +
???????????? ???"/'" + request.getParameter("sBrowseVer") + "/'," +
??????????????? "/'" + request.getParameter("iClientWidth") + "/'," +
??????????????? "/'" + request.getParameter("iClientHeight") + "/');";
??????? stmt.executeUpdate(sInsertSQL); %>
The information of other visitors:
??????? <% ResultSet RS = stmt.executeQuery("SELECT * FROM ClientMsg");
??????? while(RS.next()){ %>
???????????
???????????????
?????? ??
???????????????
Screen Width: |
?
Screen Height: |
??????
Color Depth: |
???????????????
Browse Name: |
Browse Version: |
???????????????
Client area Width: |
???????????????
Client area Height: |
?
??????? <% }
??????? RS.close();
??????? RS = null;
??????? stmt.close();
??????? cnct.close();
??????? stmt = null;
??????? cnct = null;
??? }
%>
這個(gè)Web運(yùn)行時(shí),首先用JavaScript獲得客戶端的信息,然后自動(dòng)提交頁(yè)面,提交后JSP會(huì)記錄相應(yīng)信息,并保存在數(shù)據(jù)庫(kù)里,然后再把所有的訪問(wèn)者的情況從數(shù)據(jù)庫(kù)里讀出來(lái)顯示。在這個(gè)程序里還調(diào)用了MSAgent他所顯示的文字全都是JSP的變量,當(dāng)然你也可以設(shè)置成別的。
本程序還用到了數(shù)據(jù)庫(kù),是Accsess,一個(gè)放于 %YOURAPP%/WEB-INF/msg.mdb的文件。里面有一個(gè)表ClientMsg。有幾個(gè)字段:
好的例子一個(gè)足已!
也許你會(huì)說(shuō),不就是提交么?
對(duì),下一次我們講的就是 提交的藝術(shù)。
(運(yùn)行環(huán)境 IE6、TOMCAT4.1.x JDK1.4.x)