您好,欢迎访问三七文档
当前位置:首页 > 行业资料 > 冶金工业 > JSP和JavaScript的搭配使用和相互访问
下面我们再来看一看,JavaScript对JSP方法和JSP对JavaScript的调用。这两个调用都有一个共同点,那就是需要对页面进行提交,在用Hidden设置几个隐藏参数。JSP根据获得的参数再进行相应的处理,就可以了。也许有的人已经不太明白了,那我们还是用一个程序来说明问题吧!程序二:一个服务器获得客户端信息的Web页面。他获得了客户端的一些屏幕设置,并把它们记录在数据库里。GetClientInfo.jsp%@pagecontentType=text/html;charset=utf-8language=java%%@import=java.sql.*%HTMLHEADTITLEInformation/TITLEMETAHTTP-EQUIV=Content-TypeCONTENT=text/html;charset=utf-8STYLETYPE=text/cssTABLE,BODY{font-family:CourierNew;font-size:10pt;}/STYLE/HEADBODY%if(request.getParameter(isHaveData)==null){%FORMNAME=frmClientMETHOD=POSTINPUTTYPE=HIDDENNAME=isHaveDataVALUE=INPUTTYPE=HIDDENNAME=iScreenWidthINPUTTYPE=HIDDENNAME=iColorDepthINPUTTYPE=HIDDENNAME=iScreenHeightINPUTTYPE=HIDDENNAME=sBrowseNameINPUTTYPE=HIDDENNAME=sBrowseVerINPUTTYPE=HIDDENNAME=iClientHeightINPUTTYPE=HIDDENNAME=iClientWidthSCRIPTlanguage=JavaScript1.4TYPE=TEXT/JAVASCRIPTfrmClient.iScreenWidth.value=screen.width;frmClient.iScreenHeight.value=screen.height;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{%OBJECTid=objMSAgentclassid=CLSID:D45FD31B-5C6E-11D1-9EC1-00C04FD7081Fwidth=32height=32/OBJECTSCRIPTlanguage=JavaScript1.4TYPE=TEXT/JAVASCRIPTvarMerlinID;varMerlinACS;objMSAgent.Connected=true;MerlinLoaded=LoadLocalAgent(MerlinID,MerlinACS);Merlin=objMSAgent.Characters.Character(MerlinID);Merlin.Show();Merlin.Play(Surprised);Merlin.Speak(%=Hello?%);Merlin.Play(GestureLeft);Merlin.Think(%=Thisisajspvariant%);Merlin.Play(Pleased);Merlin.Think(%=JavaScriptusesjspvariants%);Merlin.Play(GestureDown);Merlin.Speak(%=Bye!soEasy!%);Merlin.Hide();functionLoadLocalAgent(CharID,CharACS){LoadReq=objMSAgent.Characters.Load(CharID,CharACS);return(true);}/SCRIPTTABLETRTDCOLSPAN=2FONTCOLOR=#FF0000yourinfomation:/FONT/TD/TRTRTDScreenWidth:/TDTD%=request.getParameter(iScreenWidth)%/TD/TRTRTDScreenHeight:/TDTD%=request.getParameter(iScreenHeight)%/TD/TRTRTDColorDepth:/TDTD%=request.getParameter(iColorDepth)%/TD/TRTRTDBrowseName:/TDTD%=request.getParameter(sBrowseName)%/TD/TRTRTDBrowseVersion:/TDTD%=request.getParameter(sBrowseVer)%/TD/TRTRTDClientareaWidth:/TDTD%=request.getParameter(iClientWidth)%/TD/TRTRTDClientareaHeight:/TDTD%=request.getParameter(iClientHeight)%/TD/TR/TABLE%StringstrDSN=jdbc:odbc:driver={MicrosoftAccessDriver(*.mdb)};DBQ=+application.getRealPath(\\WEB-INF\\msg.mdb);Connectioncnct=null;Statementstmt=null;Class.forName(sun.jdbc.odbc.JdbcOdbcDriver);cnct=DriverManager.getConnection(strDSN,,);stmt=cnct.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);java.text.DateFormatdfNow=java.text.DateFormat.getDateTimeInstance(java.text.DateFormat.MEDIUM,java.text.DateFormat.MEDIUM);StringstrDate=dfNow.format(newjava.util.Date());StringsInsertSQL=INSERTINTOClientMsgVALUES(+\'+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);%PFONTCOLOR=#FF0000Theinformationofothervisitors:/FONT/P%ResultSetRS=stmt.executeQuery(SELECT*FROMClientMsg);while(RS.next()){%TABLETRTDCOLSPAN=2HRSIZE=1/TD/TRTRTDCOLSPAN=2STRONGFONTCOLOR=#0000FF%=RS.getString(IP)%at%=RS.getString(Time)%visitedthisPage/FONT/STRONG/TD/TRTRTDScreenWidth:/TDTD%=RS.getString(ScreenWidth)%/TD/TRTRTDScreenHeight:/TDTD%=RS.getString(ScreenHeight)%/TD/TRTRTDColorDepth:/TDTD%=RS.getString(ColorDepth)%/TD/TRTRTDBrowseName:/TDTD%=RS.getString(BrowseName)%/TD/TRTRTDBrowseVersion:/TDTD%=RS.getString(BrowseVer)%/TD/TRTRTDClientareaWidth:/TDTD%=RS.getString(ClientWidth)%/TD/TRTRTDClientareaHeight:/TDTD%=RS.getString(ClientHeight)%/TD/TR/TABLE%}RS.close();RS=null;stmt.close();cnct.close();stmt=null;cnct=null;}%BR/BODY/HTML这个Web运行时,首先用JavaScript获得客户端的信息,然后自动提交页面,提交后JSP会记录相应信息,并保存在数据库里,然后再把所有的访问者的情况从数据库里读出来显示。在这个程序里还调用了MSAgent他所显示的文字全都是JSP的变量,当然你也可以设置成别的。
本文标题:JSP和JavaScript的搭配使用和相互访问
链接地址:https://www.777doc.com/doc-314491 .html