1 / 47

ASP 动态 WEB 开发技术

ASP 动态 WEB 开发技术. ASP 是什么?. 嵌入脚本的 HTML 文件 由 WEB SERVER 解释执行. ASP 的适用环境. Windows NT Server 4.0 : MicrosoftⅡS3.0 Windows NT Workstation 4.0 : Microsoft Peer Web Services 3.0 Windows 9x : Microsoft PWS(Personal Web Server)3.0. ASP 与 CGI 、 ISAPI 的比较. Web Browser. HTTP.

pules
Download Presentation

ASP 动态 WEB 开发技术

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. ASP动态WEB开发技术

  2. ASP是什么? 嵌入脚本的HTML文件 由WEB SERVER 解释执行 ASP的适用环境 Windows NT Server 4.0 : MicrosoftⅡS3.0 Windows NT Workstation 4.0 : Microsoft Peer Web Services 3.0 Windows 9x : Microsoft PWS(Personal Web Server)3.0

  3. ASP与CGI、ISAPI的比较

  4. Web Browser HTTP Internet Information Server HTTP Web Browser ASP Engine ( ASP.DLL ) … ActiveX VBScript HTTP VBScript Web Browser Scripting ADO COM ASP脚本 DBMS ODBC ASP工作原理图

  5. 浏览器将对某个文件的浏览请求发送给诸如IIS的Web Server • Web Server根据请求的URL找出相应的文件 • Web Server从当前硬盘或内存中读取正确的文件然后将它送回用户浏览器 • 文件被用户的浏览器解释并将结果显示在用户浏览器上 静态HTML的访问流程

  6. ASP页面的访问流程 • 浏览器将一个Active Server Pages的请求发送给IIS • IIS接收这个请求并由其.asp的后缀意识到这是对一个ASP页面的请求。 • IIS从硬盘或者内存中接收正确的ASP文件 • IIS将这个ASP文件发送给一个叫ASP.DLL解释引擎 • 被请求的ASP文件将会从头至尾被执行并生成解释结果(通常是一个静态HTML页面) • 解释结果被送回浏览器 • 服务器发回的解释结果被用户浏览器解释并显示在用户浏览器上

  7. ASP的特点: 无需编译  易于生成  独立于浏览器  面向对象  兼容VBSCRIPT和JAVASCRIPT语言 利用ADO可与数据库互联 源程序码不会外漏

  8. ASP的功能举例: • 处理由浏览器传送到服务器的表单输入 • 访问和编辑服务器端的数据库表 • 读写站点服务器的文件,实现访客计数器等功能 • 提供广告轮播器、取得浏览器信息、URL表管理等内置功能 • 利用cookies与用户交互,保存用户状态 • 扩充功能的能力强,可利用VC,VB等多种开发工具定制ActiveX组件满足自己的特殊需要

  9. 第一个示例的脚本 <html> <head> <title> ASP Script 示例 </title> </head> <body> <% for i = 1 to 5 %> <font size = <%=i%> >这是第 <%=i%> 行。<br> <% next %> </body> </html>

  10. 第一个示例的解释结果 <html> <head> <title> ASP Script 示例 </title> </head> <body> <font size = 1 >这是第 1 行。<br> <font size = 2 >这是第 2 行。<br> <font size = 3 >这是第 3 行。<br> <font size = 4 >这是第 4 行。<br> <font size = 5 >这是第 5 行。<br> </body> </html>

  11. 第一个示例的运行效果

  12. 在IIS管理器中指定ASP脚本语言

  13. 使用Jscript的脚本 <% @ LANGUAGE=Jscript %> <html> <head> <title> ASP Script 示例 </title> </head> <body> <% for ( i = 1; i < 6; i++ ) { %> <font size = <%=i%> >这是第 <%=i%> 行。<br> <% } %> </body> </html>

  14. 使用<Script>标记的脚本 <html> <head> <title> ASP Script 示例 </title> </head> <body> <script language="Jscript" runat="server"> function write() { for ( i = 1; i < 6; i++ ) Response.Write( “<font size=” +i+ “>这是第” +i+ "行。<br>") } </script> <% write %> </body> </html>

  15. 在ASP中使用脚本方法的总结 • 利用 IIS 来指定作为你所有脚本中的默认语言。并使用 <% 和 %> 标记脚本 • 在每一个单独的 ASP 的第一行利用<%@ LANGUAGE = “script” %> 指定该 ASP 页面中所使用的脚本语言。 • 利用 ASP 中的 <script> 标签分别在相应位置指定多种脚本语言。

  16. 利用<%= 变量名 %>直接输出变量 <html> <head> <title> ASP Script 示例 </title> </head> <body> 今天的日期是:<%=date%> <br><br> <% response.write "现在的时间是:" & time %> </body> </html>

  17. ASP的内置对象 Request对象 用于接受从浏览器发往服务器的请求内的所有信息 Response对象 管理ASP返回浏览器的信息 Application对象 用于存储和接受可以被所有用户共享的信息。 Session对象 用于存储和接受特定用户事务信息 Server对象 允许使用服务器上各种功能函数

  18. Request对象 • 集合 • Cookies • Form • Querystring • ServerVariables • 方法 • BinaryRead • 属性 • TotalBytes

  19. Request对象的Servervariables集合举例 <HTML> <HEAD><Title>服务端变量</Title></HEAD> <BODY> <% For Each name IN Request.ServerVariables Response.write("<b>"&name&"</b>:") Response.write(Request.ServerVariables(name)) Response.write("<br>") NEXT %> </BODY> </HTML>

  20. 检查客户浏览器类型 <HTML> <HEAD><TITLE>服务端变量</TITLE></HEAD> <BODY> <% IF InStr(request.ServerVariables("HTTP_USER_AGENT"),"MSIE")=0 THEN %> <p>您使用的不是 Microsoft Internet Explorer,要浏览本页,</br> 您必须使用该浏览器,请您到<a href="www.microsoft.com">www.microsoft.com</a> 下载。 <% ELSE %> <p>您使用的是 Microsoft Internet Explorer,欢迎浏览 <% END IF %> </BODY> </HTML>

  21. <HTML> <HEAD><TITLE>注册页</TITLE></HEAD> <BODY> <% if trim(request.form("username"))="" or _ trim(request.form("usercompany"))="" then %> 您没有正确输入注册信息, 请<a href="register.html">重新填写</a> <% else %> <h4>谢谢您的注册,您的注册信息是:</h4> 姓名:<%=request.form("username")%></br> 公司:<%=request.form("usercompany")%> <% end if %> </body> </html> <HTML> <HEAD><TITLE>注册页</TITLE></HEAD> <BODY> <h4>欢迎注册,请填写以下信息:</h4> <FORM Method="post" Action="sample7.asp"> <p>请输入姓名:<input name="username" type="text"><br> 所在公司:<input name="usercompany" type="text"> <br> <input type=submit value=" 确认注册 "> </body> </html> Sample7.html Sample7.asp 获取客户的表单输入

  22. 利用QueryString获取客户输入 <HTML> <HEAD><TITLE>注册页</TITLE></HEAD> <BODY> <% FOR EACH QSParam IN Request.QueryString Response.Write("<br>"&QSParam&"=") Response.Write(Request.QueryString(QSParam)) NEXT %> </body> </html>

  23. Response对象 • 集合 • Cookies • 方法 • Write • BinaryWrite • End • 属性 • Charset • ContentType • Status

  24. 利用Response.Redirect控制流程 <HTML> <HEAD><TITLE>注册页</TITLE></HEAD> <BODY> <% if trim(request("username"))="" or _ trim(request("usercompany"))="" then response.redirect “sample10.html” else %> <h4>谢谢您的注册,您的注册信息是:</h4> 姓名:<%=request("username")%></br> 公司:<%=request("usercompany")%> <% end if %> </body> </html>

  25. Response.Redirect的工作原理 HTTP/1.1 302 Object moved Server: Microsoft-IIS/5.0 Date: Mon, 05 Jun 2000 06:26:35 GMT Location: sample10.html Connection: Keep-Alive Content-Length: 134 Content-Type: text/html Cache-control: private

  26. Response.Redirect的等效语句 <% Response.Status= “302 Object Moved” Response.AddHeader “Location”,”sample10.html” %>

  27. 在ASP中使用外置组件 <HTML> <HEAD> <TITLE> 浏览器能力检测示例 </TITLE> </HEAD> <BODY> <% Set MyBrow=Server.CreateObject("MSWC.BrowserType") %> 你的浏览器有如下特性: <P> <TABLE Border=1 Cellspacing=1> <tr> <td> 浏览器类型 </td> <td> <%=MyBrow.Browser %> </td> </tr> <tr> <td> 分屏方式 </td> <td> <%=MyBrow.Frames %> </td> </tr> <tr> <td> Cookies </td> <td> <%=MyBrow.Cookies %> </td> </tr> <tr> <td> 操作平台 </td> <td> <%=MyBrow.platform %> </td> </tr> <tr> <td> VBScript </td> <td> <%=MyBrow.vbscript %> </td> </tr> </TABLE> </BODY> </HTML>

  28. 访问服务器上的文件列表 <% root="d:\mp3" path=request("path") Set fs = CreateObject("Scripting.FileSystemObject") Set f = fs.GetFolder(root&"\"&path) Set fc = f.subfolders for each f1 in fc %> <a href=file.asp?path=<%=server.urlencode(path&"\"&f1.name)%>> <%=f1.name%></a><br> <% next Set fc = f.files for each f1 in fc %> <a href=m3u.asp?path=<%=server.urlencode(path&"\"&f1.name)%>> <%=f1.name%></a><br> <% next %>

  29. ASP 对事务的控制 什么是Session Session 的使用和处理 Session 结束的控制 Session 的事件 Session 的工作原理 Cookie Cookie是怎样工作的 不利用Cookie来保持信息 利用QueryString来保持信息 利用Form的hidden类型变量进行信息传递

  30. Session 对象 集合 Contents(Key) StaticObjects(Key) 方法 Abadon 事件 OnStart OnEnd

  31. <SCRIPT LANGUAGE=VBScript RUNAT=Server> SUB Application_OnStart application("users")=0 END SUB SUB Application_OnEnd END SUB SUB Session_OnStart application.lock application("users")=application("users")+1 application.unlock END SUB SUB Session_OnEnd application.lock application("users")=application("users")-1 application.unlock END SUB </SCRIPT> Global.asa Session 举例 <HTML><HEAD><TITLE>SESSION示例</TITLE></HEAD><BODY> <p>目前有 <%=application("users")%> 位用户在线<br><br> <% if request("action")="logout" then session.abandon response.write("您离线了") else if isempty(session("username")) and trim(request("username"))<>"" then session("username")=request("username") session("times")=1 else if isempty(session("username")) then response.write("<form method=post action=""session.asp"">") response.write("请输入您的姓名<input type=text name=""username"">") response.write("<br><input type=submit value="" 确定 "">") response.write("</form>") else response.write("您的姓名是:"&session("username")) response.write("<br>您是第 "&session("times")&" 次访问本页") response.write("<br><br><a href=""session.asp?action=logout"">注销</a>") session("times")=session("times")+1 end if end if end if %> </body></html> Session.asp

  32. ADO - OLE DB 体系结构

  33. HTML Form Internet Information Server HTML 来自远端的用户请求 对远端用户的响应 Active Server Page ( ASP ) ActiveX Data Objects ( ADO ) OLE DB Database Management System Relational Database 在 ASP 中使用 ADO 访问 Web 数据库

  34. ADO 对象模型

  35. ADO 对象模型 连接对象代表与一个数据源的唯一会话 记录集对象代表来自一个数据提供者的一组记录 域对象代表一个记录集中的一个域 命令对象代表一个命令 参数对象代表SQL存储过程或有参数查询中的一个参数 属性对象代表数据提供者的具体属性 错误对象代表ADO错误

  36. 建立数据库连接 <% Set conn=Server.CreateObject("ADODB.Connection") conn.Open "SQLSERVER","sa","" conn.defaultdatabase = "DS" %> 执行查询命令 <% set rs=Server.CreateObject("ADODB.RecordSet") rs.open "select id,name,sex,class from student " &_ " order by class, id ", conn %>

  37. 获取结果集 <% while not rs.eof %> <tr> <td><%=rs(0)%></td> <td><%=rs(“name”)%></td> <td><%=rs(2)%></td> <td><%=rs(“class”)%></td> </tr> <% rs.movenext wend %>

  38. 关闭结果集和数据库连接 <% rs.close conn.close %>

  39. 数据库查询结果

  40. 增、删、改数据 <% if request("action")="添加/修改" then id=trim(request("id")) name=trim(request("name")) sex=trim(request("sex")) class=trim(request("class")) if id <> "" and name <> "" and sex <> "" and class <> "" then conn.execute "update student set name='"& name &"', "& _ "sex='"& sex &"', "& _ "class='"& class &"' "& _ "where id='"& id &"' ",rowsaffected if rowsaffected=0 then conn.execute "insert into student(id,name,sex,class) values("& _ "'"& id &"','"& name &"','"& sex &"','"& class &"')" end if end if else if isnumeric(request("count")) then count=cint(request("count")) for i=0 to count-1 if lcase(trim(request("c"&i)))="on" then conn.execute "delete from student where id='"&trim(request("id"&i))&"'" end if next end if end if %>

  41. 添加一条记录

  42. 对结果集进行分页处理 <% set rs=Server.CreateObject("ADODB.RecordSet") rs.open "select id,name,sex,class from student order by class,id",conn,adopenstatic rs.pagesize = 3 i=0 rs.AbsolutePage=currentpage while not rs.eof and i < rs.pagesize %> <tr> <td align="left" bgcolor="#deeff7"><%=rs(0)%></td> <td align="left" bgcolor="#deeff7"><%=replace(rs("name")," ","")%></td> <td align="left" bgcolor="#deeff7"><%=rs(2)%></td> <td align="left" bgcolor="#deeff7"><%=rs("class")%></td> </tr> <% i=i+1 rs.movenext wend %>

  43. 分页处理后的数据库查询结果

  44. 搭建ASP开发环境 Windows NT Server 4.0 Windows NT Service Pack 3 Microsoft SQL Server 6.5 (可选) Micorsoft Internet Explorer 4.0 Windows NT 4.0 Option Pack Micorsoft Internet Explorer 4.01SP1 Windows NT Service Pack 4 Microsoft Visual InterDev (VS6) Remote Machine Debugging (VS6) Visual InterDev Server (VS6) Microsoft Visual Studio 6.0 Service Pack 3

  45. ASP 参考资料 迈至科 Active Server Pages 在线技术手册 ftp://qing.j32.org/pub/Documents/asp/DOC/ Working with Active Server Pages ftp://qing.j32.org/pub/Documents/asp/DOC/ MSDN Library Visual Studio 6.0 VBScript Language Reference Visual InterDev Documentation

  46. ASP 站点推荐 • 组件下载 • http://www.activex.com • 组件下载 & 编程技巧 • http://www.active.com.cn

  47. 本课结束,谢谢!

More Related