事件驅(qū)動的Web:JSP與JavaScript的融合
我們再來說一說JSP和JavaScript的搭配使用和相互訪問。
也許你用過Delphi的WebBroker開發(fā)過Web Application當你每做的一件事(提交)就是一個WebAction,說白了,就是一個函數(shù)。而這個函數(shù)在ASP、JSP條件下就成了一個頁面。不過你也應該把他們理解成函數(shù),一個返回字符串的函數(shù)。而這個字符串,就是你在瀏覽器利用查看源文件菜單命令所看到的結(jié)果。這樣,JavaScript調(diào)用Java變量的問題就迎刃而解了。我們的JSP返回的字符串有JavaScript代碼,而這些代碼是由瀏覽器解釋執(zhí)行的。由于JavaScript代碼的生成都可以由JSP動態(tài)生成,所以,JavaScript中可以包含JSP代碼的運行結(jié)果。這樣就實現(xiàn)了JavaScript對JSP變量的調(diào)用。
下面我們再來看一看,JavaScript對JSP方法和JSP對JavaScript的調(diào)用。這兩個調(diào)用都有一個共同點,那就是需要對頁面進行提交,在用Hidden設置幾個隱藏參數(shù)。JSP根據(jù)獲得的參數(shù)再進行相應的處理,就可以了。也許有的人已經(jīng)不太明白了,那我們還是用一個程序來說明問題吧!
程序二:一個服務器獲得客戶端信息的Web頁面。他獲得了客戶端的一些屏幕設置,并把它們記錄在數(shù)據(jù)庫里。
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;
??? }
%>
這個Web運行時,首先用JavaScript獲得客戶端的信息,然后自動提交頁面,提交后JSP會記錄相應信息,并保存在數(shù)據(jù)庫里,然后再把所有的訪問者的情況從數(shù)據(jù)庫里讀出來顯示。在這個程序里還調(diào)用了MSAgent他所顯示的文字全都是JSP的變量,當然你也可以設置成別的。
本程序還用到了數(shù)據(jù)庫,是Accsess,一個放于 %YOURAPP%/WEB-INF/msg.mdb的文件。里面有一個表ClientMsg。有幾個字段:
好的例子一個足已!
也許你會說,不就是提交么?
對,下一次我們講的就是 提交的藝術。
(運行環(huán)境 IE6、TOMCAT4.1.x JDK1.4.x)