LOGO OA教程 ERP教程 模切知识交流 PMS教程 CRM教程 开发文档 其他文档  
 
网站管理员

在Web页面中执行Windows程序

admin
2010年7月7日 23:36 本文热度 5281
[p]现在许多公司都面临一个难题:如何在web环境中执行存在的windows应用程序。这里就介绍实现这个功能的技术,它争取对代码做最小的改变,完成在windows环境中应做的一切。 [/p] [p][br]现存的windows应用程序[/p] [p] [/p] [p]  这里想要在web中执行的windows例子程序是非常简单的,它是用vb编写的,其中有一个表单。运行时,在表单上显示雇员的信息,这些信息来源于access数据库的一个表。表单上设有first、next、previous 和 last按钮,从而允许用户浏览记录。同时,还可以通过按钮add、delete 和 update来改变数据。这个程序通过一个com类来与数据库通信,它有下面的方法:[/p] [p]addemployee() 在表中添加一个记录,保存新雇员的信息 [br]updateemployee() 更新一个记录 [br]deleteemployee() 删除一个记录 [br]getemployees() 获取一个雇员的信息 [/p] [p] [/p] [p]程序正常运行时,浏览器显示如下:[/p] [p][br]开发web应用程序[/p] [p]  在传统的web应用程序中,大多数的处理都是在服务器端完成的。这里,我们将尝试在客户端做一些处理,以减少服务器上的工作量。也就是,让客户端完成显示信息的处理工作,并将商业规则和数据库存取留给服务器端。这就象一个n层处理模型。[/p] [p]  当用户需要访问另一个不同的数据时,我们也不想从服务器上再调入整个web页面,因此,需要找到一个web客户端在后台与web服务器交流信息的方法。这个例子中,我们使用了微软公司的xmlhttp com对象组件,它是随internet explorer 5.0而来的。当然,也可以编写一个功能类似的java applet来克服这个局限。[/p] [p] [/p] [p]服务器端的代码[/p] [p]  让我们从研究vb应用程序的com类到web的每一个方法开始,这可以通过编写asp页面来调用com类中的每个方法实现(addemployee.asp, updateemployee.asp, deleteemployee.asp, getemployee.asp)。 明白了这些,就能够在web中存取com类方法了。[/p] [p]  asp页面应该能够接受与com类一样的参数,这些页面向原始的com类发送调用。这里主要的区别就是所有的输出是以xml格式的。我们使用另外一个叫xmlconverter的com类,转换方法的输出为xml格式。xmlconverter的代码包含在下载文件中,它有一个函数,能够接受一个ado记录集做为参数,并且转换为一个xml文档。实现这个目的的函数例子可以从internet上很容易地找到,比如:[/p] [p][url=http://vbxml.com/xml/guides/developers/ado_persist_xml.asp]http://vbxml.com/xml/guides/developers/ado_persist_xml.asp[/url][/p] [p]  我们也许曾经使用过ado记录集的save函数,加上adpersistxml,来实现按照xml格式保存,但是在这里,为了简单起见,我们仍使用编制的函数。[/p] [p]下面的代码来自getemployees.asp,它执行getemployees方法,从而可以让你清晰地看到这种技术:[/p] [p][/p] [p][br]然后,用同样的方式来创建页面addemplyee.asp、deleteemployee.asp和updateemployee.asp。[/p] [p]客户端的代码[/p] [p]  现在准备编写客户端代码,首先,让我们仔细看看vb应用程序在调用后如何显示信息。在vb表单的on_load方法(参见下面的代码)中,我们调用了com对象组件getemployees方法,这个方法返回一个附带雇员信息的ado记录集。ado记录集的movefirst()、movenext()以及 movelast() 方法建立了记录浏览的方法。[/p] [p]private sub form_load()[br]'create the employeemgr object[br]set objemplyeemgr = new clsemployeemgr[/p] [p]'obtain the employee records in a adodb.recordset[br]set rst = objemplyeemgr.getemployees[br]rst.movefirst[br]displaycurrentrecord[br]end sub[/p] [p][br]   在这种情况下,我们有一个asp页面getemployees.asp,它给出了做为xml文档的信息。所以我们将在web客户端建立一个xmldom对象,并且调入由getemployees.asp提供的信息。在这个例子中,我们使用microsoft dom xml来解析。关于dom xml解析的完整文档,请参考msdn有关文章,比如 xml dom objects。[/p] [p]  另一个较好的解决方法是使用纯java/javascript,它同时可以在非internet explorer的浏览器上应用。这里,我们仍使用xmlhttp对象,它可以让web客户端建立一个到web服务器的http请求。关于对xml http的详细描述,请参考msdn上的文档。 [/p] [p]//create an xmldom on the web client machine[br]var xmldoc = new activexobject("microsoft.xmldom");[/p] [p]// node pointing at root node of the xml document[br]var noderoot;[/p] [p]// node to point at current record[br]var nodecurrentrecord;[/p] [p]// integer pointing at the current record number[br]var ncurrentindex = 0;[/p] [p][br]//create the microsoft xmlhttp object on the web client machine[br]//this would have to be present and registered on the client machine[br]//installing internet explorer 5.0 satisfies this requirement[br]var objhttprequest = new activexobject("microsoft.xmlhttp");[/p] [p]//open a http connection to the url in strurl[br]objhttprequest.open ("get", strurl, false, null, null);[/p] [p]//send the request[br]objhttprequest.send();[/p] [p]//obtain the response received to the xmlresponse variable[br]//this response would be the xml document returned by the web server[br]var xmlresponse = objhttprequest.responsetext[/p] [p]//since the response is an xml document we can load it to an xmldoc object[br]xmldoc.loadxml (xmlresponse);[/p] [p]//set noderoot to point at the root of the xml document[br]noderoot = xmldoc.documentelement;[/p] [p][br]   从上面我们了解了xml文档的结构,现在可以仔细研究xml文档对象了。我们将编写一个客户端的javascript函数 rstmovefirst(),它可以移动当前记录指针到第1条,这与ado记录集的movefirst方法类似:[/p] [p]function rstmovefirst() {[br]//error trap for empty record set[br]if (noderoot.childnodes.length; < 1) {[/p] [p]//if the root node does not have any child nodes then there are[br]//no "records"[br]return false;[br]} [br]ncurrentindex = 0;[/p] [p]//set the nodecurrentrecord to point at the 0th child of the [br]//xml document. the 0th child would be the first record.[br]// noderoot is the xml document? documentelement[br]nodecurrentrecord = noderoot.childnodes(ncurrentindex);[/p] [p]//return success[br]return true;[br]} [/p] [p][br]   同样,我们可以编写rstmovenext()和 rstmovelast()函数,通过编写这些代码,我们将能仔细地了解xml文档元素。而且,再编写一个类似于ado记录集upadte方法的函数。[/p] [p]  现在我们在客户机上创建了一个假冒的ado记录集对象,因此就可以象在vb应用程序中一样来处理这些“记录集”。[/p] [p]  有了这些函数,剩下的就是编写用户界面,这就象在vb应用程序中一样。比如,在vb应用程序中,“移动到第1条记录”后面的代码是:[/p] [p]private sub btnfirst_click()[br]if not (rst.eof and rst.bof) then[/p] [p]'move to the first record[br]rst.movefirst[/p] [p]'displaycurrentrecord is a function that display the current 'records information [br]displaycurrentrecord[/p] [p]end if[br]end sub[/p] [p][br]在web应用程序中,相应的代码是:[/p] [p]function btnfirstclick() {[br]'move to the first record in the recordset[br]'note that our rstmovefirst returns true if[br]'it was successful and false if eof and bof [br]if (rstmovefirst()) {[br]'here displaycurrentrecord is client side javascript[br]'function that display the current records information on[br]'the the screen[br]displaycurrentrecord();[br]}[br]}[/p] [p]  当需要更新实际的数据库时,就发送更新信息给updateemployee.asp,这个页面将通过com对象的updateemployee方法来更新数据库。上面描述的应用程序,输出到web上,将显示如下图:[/p] [p][br]结论[/p] [p]  在web应用中,要建立适当的计划来转换ado记录集为xml文档。一旦定义明确了,象那样标准的应用将很好实现。另外一个我们想研究的领域是客户端记录集的操纵函数(就是rst*函数)。我们可以编写java applet来处理这些记录集操纵函数,从而在客户端建立了java记录集对象。这种方法将是很面向对象的一种处理方法。[/p] [p]点击此处下载本文相关资料:[br][url=http://asptoday.com/articles/images/20000602.zip]http://asptoday.com/articles/images/20000602.zip[/url][/p]

该文章在 2010/7/7 23:36:08 编辑过
关键字查询
相关文章
正在查询...
点晴ERP是一款针对中小制造业的专业生产管理软件系统,系统成熟度和易用性得到了国内大量中小企业的青睐。
点晴PMS码头管理系统主要针对港口码头集装箱与散货日常运作、调度、堆场、车队、财务费用、相关报表等业务管理,结合码头的业务特点,围绕调度、堆场作业而开发的。集技术的先进性、管理的有效性于一体,是物流码头及其他港口类企业的高效ERP管理信息系统。
点晴WMS仓储管理系统提供了货物产品管理,销售管理,采购管理,仓储管理,仓库管理,保质期管理,货位管理,库位管理,生产管理,WMS管理系统,标签打印,条形码,二维码管理,批号管理软件。
点晴免费OA是一款软件和通用服务都免费,不限功能、不限时间、不限用户的免费OA协同办公管理系统。
Copyright 2010-2024 ClickSun All Rights Reserved