Asp深度揭密(下)

【字号: 日期:2022-10-21浏览:36作者:雯心
四、Asp组件的开发与使用:1.;;;;组件的特点?l;;;;优点:n;;;;调用方便,节省代码n;;;;安全性高n;;;;支持事务处理,多组件联合n;;;;运行速度快n;;;;升级、修改组件不需修改页面,因此扩展性好l;;;;缺点:n;;;;开发及调试困难2.;;;;如何使用VB开发?⑴.打开VB>>New Project>>ActiveX DLL⑵.修改项目名称为course⑶.修改类模块的名字为conn_db⑷.Project>> References,引用COM+ Service Type Library和Microsoft Active Server Pages Object Library。⑸.修改类代码如下:from www.mypchelp.cn'建立数据库连接并输出数据库字段Dim Response As ResponseDim Request As RequestDim Server As ServerDim Application As ApplicationDim Session As Session;;;;Private Sub Class_Initialize()Dim objContext As ObjectContextSet objContext = GetObjectContext()Set Response = objContext("Response")Set Request = objContext("Request")Set Server = objContext("Server")Set Application = objContext("Application")Set Session = objContext("Session")End SubSub conn_db()Set conn = CreateObject("adodb.connection")conn.open "course_dsn", "course_user", "course_password"Set rs = CreateObject("adodb.recordset")rs.open "select * from user_info", conn, 1, 1If rs.recordcount > 0 ThenFor i = 1 To rs.recordcountResponse.write "<br>" & rs("user_name") & "<br>"If rs.EOF Then Exit Forrs.movenextNextEnd Ifrs.CloseSet rs = Nothingconn.CloseSet conn = NothingEnd Sub⑹.添加一新类cutstr⑺.修改类代码如下:'截取字符串Function cutstr(str, length)If Len(str) > length Thencutstr = Left(str, length) & "..."Elsecutstr = strEnd IfEnd Function⑻.File>>Save⑼.File>>make course.dll3.;;;;注册组件:MTS和regsvr32.exe;;;;有两种方式注册组件:MTS和使用regsvr32.exe。MTS是值得推荐的,因为它具有下列优点:n;;;;动态卸载平衡,提高组件和基于组件的应用程序的升级性。n;;;;包含公布和提交事件和队列组件的能力,使得更容易与多个组件联合。要想使组件具有MTS的特性,必须对组件做少许改动。在NT和98下开发时,必须在项目中引用Microsoft Transaction Server Type Library,在Windows 2000下开发,必须引用COM+ Service Type Library。⑴.regsvr32注册:regsvr32.exe是system32下面的一个可执行文件,它将组件信息读入注册表,以便Asp调用。使用命令行进入组件dll文件所在的目录,执行“regsvr32 dll_file_name”即可。运行regedit,在HKEY_CLASSES_ROOT下就会找到course.conn_db项和course.cutstr项,表明组件注册成功。⑵.使用MTS注册:①.开始>>程序>>管理工具>>组件服务②.展开目录至如下状态:③.按照向导,下一步,直到如下对话框,点击“创建一个空的应用程序”:④.在接下来的对话框中,为应用程序起名为“course”,其他默认,直至完成⑤.展开course应用程序,右键,新建一个组件⑥.按照提示,继续,出现如下对话框时,选择“导入已被注册的组件”⑦.选择我们开发的组件,下一步,直至完成⑧.这时候,可以发现course应用程序下已经多了两个组件:4.;;;;在Asp中调用组件asp_use_com.asp<%'asp调用com组件set cutstr_obj=server.createobject("course.cutstr")response.write cutstr_obj.cutstr("abcdefghijk",3)&"<br>"set cutstr_obj=nothingset conn_obj=server.createobject("course.conn_db")conn_obj.conn_db()set conn_obj=nothing%>效果:abc...ahyituth说明调用成功。5.;;;;卸载组件⑴.使用regsvr32注册的组件,使用-u开关卸载:注意:先进入组件dll所在的目录,然后使用“regsvr32 –u dll_file_name”卸载;卸载后重启IIS即可。⑵.使用MTS注册的组件,先在“组件服务”中删除对应的应用程序,然后再执行步骤 ⑴ 以彻底卸载组件。6.;;;;Dll组件存放位置和权限设定⑴.我们只需要把编译生成的Dll文件拷贝出来即可,其他的文件不用做处理⑵.要把Dll放到Web站点之外,如system32目录里,防止被下载⑶.Dll的文件权设置为System读取,Internet用户遍历文件夹/运行文件⑷.Dll在IIS中去掉所有的权限,如读取,脚本自愿访问等经过上述处理,可以确保Dll文件的安全。7.;;;;其他如何在组件中使用Asp的对象以方便的将Asp代码移植为COM组件?五、IIS最优化配置1.Web站点选项卡:IP、端口、虚拟主机、连接、日志2.ISAPI筛选器:加入PHP和JSP支持3.主目录配置选项卡:I IS权限设定(结合文件权)、执行许可、应用程序保护、映射、缓冲、父路径、出错信息4.其他选项卡:自定义错误、Http头、目录安全性、文档5.文件压缩带来的好处和坏处六、其他1.;;;;发送邮件(JMail;Ms smtp)使用Microsoft Smtp发送电子邮件⑴.安装Microsoft SMTP Service⑵.设置Microsoft SMTP Service⑶.代码部分:mail_smtp.asp<%sub sendmail(fromwho,towho,subject,body)dim mymailset mymail = server.createobject("cdonts.newmail")mymail.from = fromwhomymail.to = towhomymail.subject = subjectmymail.body = bodymymail.sendset mymail = nothingend sub%>该子程序接受4个与下列各条对应的参数。l;;;;邮件发送者的email地址l;;;;邮件接收者的email地址l;;;;邮件主题l;;;;邮件内容使用方法:<%fromWho=…toWho=…Subject=…Body=…IF toWho <> "" THENsendMail fromWho, toWho, Subject, BodyEND IF%>使用Jmail发送Email略,有兴趣可以和我探讨,Jmail这个软件我也有。2.;;;;解压Zip文件(Wscript.Shell和Winzip command line;Java组件)⑴.安装Winzip 8.1以上⑵.安装Winzip command line⑶.将工作目录的文件权设置为Internet用户可以读取、写入和修改⑷.代码部分:unzip_a_zipfile.asp<%'用shell对象启动程序'zip_path是具体zip文件的路径,如c:test.zip'path是存放解压后文件的路径'ond是命令行参数set wshshell = server.createobject("wscript.shell")issuccess = wshshell.run ("wzunzip -ond "&zip_path&" "&path,1,true)'删除zip文件set myfileobject=server.createobject("scripting.filesystemobject")myfileobject.deletefile zip_path'判断是否成功以继续操作if issuccess = 0 then ;;;'成功;;;...else;;;'失败;;;;...end if%>3.;;;;操作XML文件本次交流时间有限,有时间再做详细探讨4.文件上传⑴.安装文件上传组件Asp fileup(支持多文件上传,文件类型及大小判断,文件上传后改名等)⑵.重起IIS以使上传组件生效⑶.设置上传目录的文件权为Internet用户可以读取、写入和修改⑷.代码部分upload_file.htm<style type="text/css"><!--.input {background-color: #FFFFFF; border-bottom: black 1px solid;border-left: black 1px solid; border-right: black 1px solid;border-top: black 1px solid; color: #000000;font-family: Georgia; font-size: 9pt;color: midnightblue;}a:link {color: #1B629C; text-decoration: none}a:hover {color: #FF6600; text-decoration: underline}a:visited {text-decoration: none}--></style><center><form enctype="multipart/form-data" method="post" action="upload_file.asp" name="Upload"><input type="hidden" name="CopyrightInfo" value="http://www.chinaasp.com">请选择文件:<input type="file" name="file1" class="input"><br><br>请选择文件:<input type="file" name="file2" class="input"><br><br></form><br><br><a href="javascript:document.Upload.submit();"> 提 交 </a></center>upload_file.asp<%on error resume next'定义获得文件后缀的函数function getfileextname(filename)pos=instrrev(filename,".")if pos>0 then ;;;;getfileextname=mid(filename,pos+1)else;;;;getfileextname=""end ifend function'定义获取文件正名的函数function getfilename(filename)lens=len(filename)-len(getfileextname(filename))-1getfilename=left(filename,lens)end function'创建文件上传组件的对象set fileup=server.createobject("chinaasp.upload")'循环读取用户上传的文件,并保存在服务器上for each f in fileup.files'当用户没有选择文件或文件大小超过10m时返回到选择上传文件的页面if f.filename="" or f.filesize>10485500 then response.redirect "upload_file.htm"'获取保存的路径path=server.mappath("upload_file.asp")path=left(path,len(path)-15)'保存文件f.saveas path&getfilename(f.filename)&"."&getfileextname(f.filename)nextresponse.redirect "upload_file.htm"%>5.驱动器/目录/文件操作本次交流时间有限,有时间再做详细探讨6.;;;;Asp编写与调试经验:cookies和session如何选择、cookies数量陷阱、页面过期和缓冲设定、移植性如何保证、如何应付内部服务器500错误……1.Cookies和Session的选择:⑴.共同特点⑵.不同之处:①.工作方式②.过期条件③.对服务器的性能影响2.Cookies数量陷阱:IIS可以保存一般的cookies不超过20个,再定义新的Cookies以前的Cookies的值就丢失了,这样对大型应用显然局限性非常大,如何解决这个问题呢?答案是使用二维Cookies。例子:;;;;测试一维Cookies数量极限:;;;;test_cookies_1.asp<%for i=1 to 50response.cookies("cookies_"&i)=inext%>test_cookies_2.asp;;;;<%for i=1 to 50response.write request.cookies("cookies_"&i)&"<br>"next%>效果:先访问test_cookies_1.asp,再访问test_cookies_2.asp,,发现了什么?test_cookies_3.asp<%for i=1 to 50response.cookies("cookies_"&i)=inextfor i=1 to 50response.write request.cookies("cookies_"&i)&"<br>"next%>效果:没有Cookies丢失!!!!测试二维Cookies数量极限:test_cookies_4.asp<%for i=1 to 301response.cookies("tuht")("cookies_"&i)=inext%>test_cookies_5.asp<%for i=1 to 301response.write request.cookies("tuht")("cookies_"&i)&"<br>"next%>效果:使用这种方式可以使用201*20=4020个Cookies!!!!3.页面过期和缓冲设定<%'过期和缓冲处理response.buffer=trueresponse.cachecontrol="no-chache"response.expiresabsolute=now()-1response.expires=0%>html中还可以做设定:<meta content="no-cache" http-equiv="Pragma"><meta HTTP-EQUIV="Expires" CONTENT="0">4.移植性的保证⑴.包含文件<!--#include file="top.asp" -->⑵.使用server.mappath寻找文件路径,避免在页面中直接使用绝对路径⑶.尽量使用组件封装业务逻辑;;;;5.调试内部服务器500的错误⑴.设置IIS显示具体的错误信息⑵.分步调试,由上而下;;;;⑶.打印某些重要的变量的值,检查是否为我们预期⑷.根据经验来判断错误7.;;;;操作Word文档⑴.安装Office 2000,其中Word 2000必选⑵.设置IE中Internet的安全性:ActiveX控件和插件全部启用⑶.设置工作目录的文件权为Internet及System读取/修改/写入⑷.编写模版course.dot⑸.具体代码:opr_doc_inc.asp<%Response.write "Dim Var_Num" & chr(13)Response.write " Var_Num = 2 " & chr(13)Response.write "Dim varstrings(2)" & chr(13)Response.write "varstrings(0)=" & chr(34) & "起草人:" & chr(34) & chr(13)Response.write "varstrings(1)=" & chr(34) & "日期:" & chr(34) & chr(13)Response.write "Dim varValues(2)" & chr(13)Response.write "varValues(0)=" & chr(34) &"起草人:涂海涛"& chr(34) & chr(13)Response.write "varValues(1)=" & chr(34) & "日期:"&date()& chr(34) & chr(13)%>Sub instead(word);;;Set myRange = word.ActiveDocument.Content;;;for i=0 to Var_Num - 1 ;;;;;;;call myRange.Find.Execute(varStrings(i),false,false,false,false,false,false,false,false,varValues(i),2);;;NextEnd Subopr_doc.asp<%'获取保存的路径path=server.mappath("opr_doc.asp")path=left(path,len(path)-11)filenames=path&"test.doc"w1="word.activedocument.saveAs"&chr(32)&chr(34)&filenames&chr(34)w2="wApp.Documents.open"&chr(32)&chr(34)&filenames&chr(34)%><script language="vbscript">On Error Resume Next'生成指定文件名的Word文档Dim wordset word = CreateObject("Word.Application")if Err.number > 0 ThenAlert "发生错误,请确认文件是否存在"elseword.visible = Falseword.documents.open "<%response.write path%>course.dot"<%Response.write w1%>word.documents.closeset word=nothingend if<!--#include file="opr_doc_inc.asp"-->Dim wAppSet wApp = CreateObject("Word.Application")If Err.number > 0 ThenAlert "发生错误,请确认文件是否正确创建"elsewApp.visible = True<%Response.write w2%>call instead(wApp)set wApp=nothingend if</script>效果:看看生成了doc文件吗?这个新建的doc文件和模版文件有什么区别?起草人和日期发生了变化了吗?保存一下,看看新生成的doc文件的内容。附:1.以上全部代码在Windows 2000 Server SP2+IIS 5.0+MS SQL Server 2000+Office 2000下测试通过2.配置数据库:数据库名course,用户course_user,密码course_password,ODBC驱动为course_dsn,端口为2433,描述表结构的脚本在共享目录下。3.Asp fileup、Jmail、Winzip 8.1、Winzip command line这几个软件请自行下载。4.数据库脚本文件:if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[output_1]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)drop procedure [dbo].[output_1]GOif exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[return_1]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)drop procedure [dbo].[return_1]GOif exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[user_info_1]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)drop procedure [dbo].[user_info_1]GOif exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[user_info_2]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)drop procedure [dbo].[user_info_2]GOif exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[user_info_3]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)drop procedure [dbo].[user_info_3]GOif exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[user_info]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)drop table [dbo].[user_info]GOCREATE TABLE [dbo].[user_info] (;;;;[id] [int] IDENTITY (1, 1) NOT NULL ,;;;;[user_name] [varchar] (40) COLLATE Chinese_PRC_CI_AS NOT NULL ,;;;;[password] [varchar] (20) COLLATE Chinese_PRC_CI_AS NOT NULL ) ON [PRIMARY]GOALTER TABLE [dbo].[user_info] WITH NOCHECK ADD ;;;;CONSTRAINT [PK_user_info] PRIMARY KEY;;CLUSTERED ;;;;([user_name];;;;);;ON [PRIMARY] GOSET QUOTED_IDENTIFIER OFF GOSET ANSI_NULLS OFF GOCREATE PROCEDURE [output_1]@sid int outputASset @sid=2GOSET QUOTED_IDENTIFIER OFF GOSET ANSI_NULLS ON GOSET QUOTED_IDENTIFIER OFF GOSET ANSI_NULLS OFF GOCREATE PROCEDURE [return_1](@user_name varchar(40),@password varchar(20))ASif exists(select id from user_info where user_name=@user_name and password=@password)return 1elsereturn 0GOSET QUOTED_IDENTIFIER OFF GOSET ANSI_NULLS ON GOSET QUOTED_IDENTIFIER ON GOSET ANSI_NULLS OFF GOCREATE PROCEDURE [user_info_1](@user_name varchar(40),@password varchar(20))ASselect id from user_info where user_name=@user_name and password=@passwordGOSET QUOTED_IDENTIFIER OFF GOSET ANSI_NULLS ON GOSET QUOTED_IDENTIFIER OFF GOSET ANSI_NULLS OFF GOCREATE PROCEDURE [user_info_2](@user_name varchar(40),@password varchar(20))ASSET XACT_ABORT ONBEGIN TRANSACTIONdelete from user_info where user_name=@user_name and password=@passwordCOMMIT TRANSACTIONSET XACT_ABORT OFFGOSET QUOTED_IDENTIFIER OFF GOSET ANSI_NULLS ON GOSET QUOTED_IDENTIFIER OFF GOSET ANSI_NULLS OFF GOCREATE PROCEDURE [user_info_3] ASselect * from user_infoGOSET QUOTED_IDENTIFIER OFF GOSET ANSI_NULLS ON GO
相关文章: