Event-driven Web journey-Integration of JSP and JavaScript (this article is dedicated to all programmers who use Delphi). Before that, let me write a sequence! I love Delphi deeply and cannot extricate myself. However, I also saw that B/S is the general direction of future program design. With Delphi for many years, I chose Java/jsp at this time (do you know? The first language to try cross-platform development is Pascal ). To be honest, Delphi deserves to be the leader in the Windows desktop development field. He is so fast and convenient. When I learned the web, I also introduced the idea of Delphi. why can't the web be like Windows application? Event-driven status is automatically maintained (this is actually the idea of windows ). However, after learning web development, you know that when you want to make page changes known to the server, you must submit the changes to change the page status. Of course, this also increases the complexity of maintaining the page status. But it doesn't matter. Let's take a while. When JSP and JavaScript are integrated, the effect of JSP and JavaScript is the same for users? Maybe someone throws eggs now. However, how can we develop an ideal Application Suitable for users? This is of course from the user's perspective. From the programmer's point of view, many people will laugh at me just now, but for users, no matter how much code you have, no matter what database support you have, whether you use Bubble sorting or quick sorting, they only care about face issues. This article does not discuss art design. I want JSP and JavaScript to achieve the same effect, but this is for users. I have also seen on the csdn forum whether JSP variables can be called by JavaScript. javascript can control the question of JSP. The answer is usually two: the first one is a straightforward answer: "No, there are two things at all. "The second one is simply more sincere." This one is the server, and the other is the client. How can this happen? Let's look at the book ". As a matter of fact, this is a very worthy question. Let's take a look at the same effect of JSP and JavaScript from the user's perspective. Program 1: A direction control program with five buttons in the browser. Click the button in the middle of the above button to move up, click the button in the middle of the following to move down ...... Similarly, click the middle button to restore the initial state. Let's take a look at the JSP code: fivebuttons. jsp
<% @ Page contenttype = "text/html; charset = UTF-8" Language = "Java" %> <% @ page import = "Java. SQL. * "%> <% int ileft; int iTOP; If (request. getparameter ("hidl ")! = NULL) {ileft = integer. parseint (request. getparameter ("hidl"); iTOP = integer. parseint (request. getparameter ("hidt"); If (request. getparameter ("btnu ")! = NULL & request. getparameter ("btnu"). compareto ("U") = 0) iTOP-= 10; If (request. getparameter ("btnd ")! = NULL & request. getparameter ("btnd"). compareto ("D") = 0) iTOP + = 10; If (request. getparameter ("btnl ")! = NULL & request. getparameter ("btnl"). compareto ("L") = 0) ileft-= 10; If (request. getparameter ("btnr ")! = NULL & request. getparameter ("btnr"). compareto ("R") = 0) ileft + = 10; If (request. getparameter ("btnm ")! = NULL & request. getparameter ("btnm "). compareto ("M") = 0) {ileft = 400; iTOP = 200 ;}} else {ileft = 400; iTOP = 200 ;} %> <HTML> |
Let's take a look at the JavaScript code: fivebuttons.htm
<HTML> <script language = "javascript1.2" type = "text/JavaScript"> function btnclick (s) {If (S = "U") This. btnm. style. top = parseint (this. btnm. style. top)-10; If (S = "D") This. btnm. style. top = parseint (this. btnm. style. top) + 10; If (S = "L") This. btnm. style. left = parseint (this. btnm. style. left)-10; If (S = "R") This. btnm. style. left = parseint (this. btnm. style. left) + 10; If (S = "M") {This. btnm. style. top = 200; this. btnm. style. left = 400 ;}</SCRIPT> |
Let's talk about the use and mutual access of JSP and javas pipeline. You may have used Delphi webbroker to develop Web applications. When every task you do (submit) is a webaction, it is a function. This function becomes a page under ASP and JSP conditions. However, you should also regard them as functions, a function that returns strings. This string is the result you can see in the browser by using the menu command to view the source file. In this way, the javas container can solve the problem of calling Java variables. The strings returned by our JSP include the javas pipeline code, which is interpreted and executed by the browser. Because the javas pipeline code can be dynamically generated by JSP, javas pipeline can contain the JSP code running result. In this way, javas container calls JSP variables. Next let's take a look at the javas pipeline call to the JSP method and JSP to the javas pipeline. The two calls have one thing in common, that is, the page needs to be submitted, and several hidden parameters need to be set with hidden. JSP can process the obtained parameters accordingly. Maybe some people don't quite understand, so let's use a program to explain the problem! Procedure 2: A web page on which a server obtains client information. He obtained some Screen Settings from the client and recorded them in the database. Getclientinfo. jsp
<% @ Page contenttype = "text/html; charset = UTF-8" Language = "Java" %> <% @ import = "Java. SQL. * "%> <HTML> Frmclient. icolordepth. value = screen. colordepth; frmclient. sbrowsename. value = navigator. appname; frmclient. sbrowsever. value = navigator. appversion;Frmclient. iclientheight. value = screen. availheight; frmclient. iclientwidth. value = screen. availwidth; frmclient. submit (); </SCRIPT> </form> <%} else {%> <Object ID = objmsagentclassid = CLSID: d45FD31B-5C6E-11D1-9EC1-00C04FD7081F width = "32" Height = "32"> </Object> <script language = "javascript1.4" type = "text/JavaScript"> var merlinid; var merlinacs; objmsagent. connected = true; merlinloaded = loadlocalag ENT (merlinid, merlinacs); Merlin = objmsagent. characters. character (merlinid); Merlin. show (); Merlin. play ("surprised"); Merlin. speak ("<% =" Hello? "%>"); Merlin. play ("gestureleft"); Merlin. think ("<% =" this is a JSP variant "%>"); Merlin. play ("pleased"); Merlin. think ("<% =" javascript uses JSP variants "%>"); Merlin. play ("gesturedown"); Merlin. speak ("<% =" Bye! So easy! "%>"); Merlin. hide (); function loadlocalagent (charid, characs) {loadreq = objmsagent. characters. load (charid, characs); Return (true );} </SCRIPT> <Table> <tr> <TD colspan = "2"> <font color = "# ff0000"> your infomation: </font> </TD> </tr> <TD> screen width: </TD> <% = request. getparameter ("iscreenwidth") %> </TD> </tr> <TD> screen height: </TD> <% = request. getparameter ("iscreenheight") %> </TD> </tr> <TD> color depth: </TD> <% = request. getparameter ("icolordepth") %> </TD> </tr> <TD> browse name: </TD> <% = request. getparameter ("sbrowsename") %> </TD> </tr> <TD> browse version: </TD> <% = request. getparameter ("sbrowsever") %> </TD> </tr> <TD> client area width: </TD> <% = request. getparameter ("iclientwidth") %> </TD> </tr> <TD> client area Height: </TD> <% = request. getparameter ("iclientheight") %> </TD> </tr> </table> <% string strdsn = "JDBC: ODBC: Driver = {Microsoft Access Driver (*. MDB)}; DBQ = "+ application. getrealpath ("// 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.exe cuteupdate (sinsertsql); %> <p> <font color = "# ff0000"> the information of other visitors: </font> </P> <% resultset rs = stmt.exe cutequery ("select * From clientmsg"); While (RS. next ()) {%> <Table> <tr> <TD colspan = "2"> <HR size = "1"> </TD> </tr> <TD colspan = "2"> <strong> <font color = "# 0000ff"> <% = Rs. getstring ("ip") %> at <% = Rs. getstring ("time") %> visited this page </font> </strong> </TD> </tr> <TD> screen width: </TD> <% = Rs. getstring ("screenwidth") %> </TD> </tr> <TD> screen height: </TD> <% = Rs. getstring ("screenheight") %> </TD> </tr> <TD> color depth: </TD> <% = Rs. getstring ("colordepth") %> </TD> </tr><Tr> <TD> browse name: </TD> <% = Rs. getstring ("browsename") %> </TD> </tr> <TD> browse version: </TD> <% = Rs. getstring ("browsever") %> </TD> </tr> <TD> client area width: </TD> <% = Rs. getstring ("clientwidth") %> </TD> </tr> <TD> client area Height: </TD> <% = Rs. getstring ("clientheight") %> </TD> </tr> </table> <%} Rs. close (); RS = NULL; stmt. close (); cnct. close (); stmt = NULL; cnct = NULL; }%> <Br> </body>
A good example! Maybe you will say, isn't it a commit? Yes, next time we will talk about the art of submission. (Run IE6 and atat4.1.x jdk1.4.x) |