asp(active server page)是microsoft公司推出的基于pws(personal web server)&iis(inte.netinformation server)平台的、基于isapi(inte.netserviceapi)原理的动态网页开发技术,目前日趋成熟完善。在这里仅就代码优化进行一些简单讨论。
1、 声明vbscript变量
在asp中,对vbscript提供了强劲的支持,能够无缝集成vbscript的函数、方法,这样给扩展asp的现有功能提供了很大便利。由于asp中已经模糊了变量类型的概念,所以,在进行asp与vbscript交互的过程中,很多程序员也惯于不声明vbscript的变量,这样加重了服务器的解析负担,进而影响服务器的响应请求速度。
鉴于此,我们可以象在vb中强制用户进行变量声明一样在vbscript中强制用户进行变量声明。实现方法是在asp程序行首放置<% optionexplicit%>。
2、 对url地址进行编码
在我们使用asp动态生成一个带参数url地址并进行跳转时,在ie中解析很正常,但.netscrape浏览时却有错误如下:
http error 400
400 bad request
due to malformed syntax, the request could not be understood by the server.
the client should not repeat the request without modifications.
解决方法是对生成的url参数使用asp内置server对象的urlencode方法进行url编码,例子如下:
<%
url="xur.asp"
var1="username=" & server.urlencode("xur")
var2="&company=" & server.urlencode("xurstudio")
var3="&phone=" & server.urlencode("021-53854336-186")
response.redirect url & "?" & var1 & var2 & var3
%>
3、 清空对象
当使用完对象后,首先使用close方法来释放对象所占用的系统资源;然后设置对象值为“nothing”释放对象占用内存。当年,我就是在一张页面上创建了百余个没有清空对象的记录集而崩溃了我的iis。下面的代码使用数据库内容建立一个下拉列表。代码示例如下:
<% mydsn="dsn=xur;uid=xur;pwd=xur"
mysql="select * from authors where au_id<100"
set conntemp=server.createobject("adodb.connection")
conntemp.open mydsn
set rstemp=conntemp.execute(mysql)
if rstemp.eof then
response.write "数据库为空"
response.write mysql
conntemp.close
set conntemp=nothing
response.end
end if%>
<%do until rstemp.eof %>
<%
rstemp.movenext
loop
rstemp.close
set rstemp=nothing
conntemp.close
set conntemp=nothing
%>
4、使用字符串建立sql查询
使用字符串来建立查询并不能加快服务器的解析速度,相反,它还会增加服务器的解析时间。但在这里仍然推荐使用字符串代替简单的查询语句来进行查询。这样做的好处是,可以迅速发现程序问题所在,从而便利高效地生成程序。示例如下:
<%mysql= ""select * "
mysql= mysql & "from publishers"
mysql= mysql & "where state=’ny’"
response.write mysql
set rstemp=conntemp.execute(mysql)
rstemp.close
set rstemp=nothing
%>
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 注册表 操作系统 服务器 应用服务器