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 50
response.cookies("cookies_"&i)=i
next
%>
test_cookies_2.asp
<%
for i=1 to 50
response.write request.cookies("cookies_"&i)&"<br>"
next
%>
效果:
先访问test_cookies_1.asp,再访问test_cookies_2.asp,,发现了什么?
test_cookies_3.asp
<%
for i=1 to 50
response.cookies("cookies_"&i)=i
next
for i=1 to 50
response.write request.cookies("cookies_"&i)&"<br>"
next
%>
效果:
没有cookies丢失!!!!
测试二维cookies数量极限:
test_cookies_4.asp
<%
for i=1 to 301
response.cookies("tuht")("cookies_"&i)=i
next
%>
test_cookies_5.asp
<%
for i=1 to 301
response.write request.cookies("tuht")("cookies_"&i)&"<br>"
next
%>
效果:
使用这种方式可以使用201*20=4020个cookies!!!!
3.页面过期和缓冲设定
<%
'过期和缓冲处理
response.buffer=true
response.cachecontrol="no-chache"
response.expiresabsolute=now()-1
response.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中inte.net的安全性:activex控件和插件全部启用
⑶.设置工作目录的文件权为inte.net及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)
next
end sub
opr_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 word
set word = createobject("word.application")
if err.number > 0 then
alert "发生错误,请确认文件是否存在"
else
word.visible = false
word.documents.open "<%response.write path%>course.dot"
<%response.write w1%>
word.documents.close
set word=nothing
end if
<!--#include file="opr_doc_inc.asp"-->
dim wapp
set wapp = createobject("word.application")
if err.number > 0 then
alert "发生错误,请确认文件是否正确创建"
else
wapp.visible = true
<%response.write w2%>
call instead(wapp)
set wapp=nothing
end 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]
go
if exists (select * from dbo.sysobjects where id = object_id(n'[dbo].[return_1]') and objectproperty(id, n'isprocedure') = 1)
drop procedure [dbo].[return_1]
go
if 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]
go
if 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]
go
if 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]
go
if exists (select * from dbo.sysobjects where id = object_id(n'[dbo].[user_info]') and objectproperty(id, n'isusertable') = 1)
drop table [dbo].[user_info]
go
create 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]
go
alter table [dbo].[user_info] with nocheck add
constraint [pk_user_info] primary key clustered
(
[user_name]
) on [primary]
go
set quoted_identifier off
go
set ansi_nulls off
go
create procedure [output_1]
@sid int output
as
set @sid=2
go
set quoted_identifier off
go
set ansi_nulls on
go
set quoted_identifier off
go
set ansi_nulls off
go
create procedure [return_1]
(@user_name varchar(40),@password varchar(20))
as
if exists(select id from user_info where user_name=@user_name and password=@password)
return 1
else
return 0
go
set quoted_identifier off
go
set ansi_nulls on
go
set quoted_identifier on
go
set ansi_nulls off
go
create procedure [user_info_1]
(@user_name varchar(40),@password varchar(20))
as
select id from user_info where user_name=@user_name and password=@password
go
set quoted_identifier off
go
set ansi_nulls on
go
set quoted_identifier off
go
set ansi_nulls off
go
create procedure [user_info_2]
(@user_name varchar(40),@password varchar(20))
as
set xact_abort on
begin transaction
delete from user_info where user_name=@user_name and password=@password
commit transaction
set xact_abort off
go
set quoted_identifier off
go
set ansi_nulls on
go
set quoted_identifier off
go
set ansi_nulls off
go
create procedure [user_info_3] as
select * from user_info
go
set quoted_identifier off
go
set ansi_nulls on
go
Java Asp PHP .Net XML C/C++ CGI VB Jsp J2ee J2se J2me EJB Servlet Tomcat Resin Struts Weblogic Eclipse ANT GUI JMS Web servise IDEA Webphere Hibernate Spring Jboss Applet Swing Socket Javamail Perl Ajax P2P 安全 模式 框架 测试 开源 游戏
Windows XP Windows 2000 Windows 2003 Windows Me Windows 9.x Linux UNIX 注册表 操作系统 服务器 应用服务器