第 8 章 Web 编程技术

43
8 8 Web Web 第第第第 第第第第

description

第 8 章 Web 编程技术.  本章学习目标:. 1 .通过本章的学习,了解 JavaScript 脚本的编写方法; 2 .能使用 JavaScript 编写简单的脚本; 3 .了解什么是 ASP 、 JSP ,它们同 JavaScript 的关系如何; 4 .理解 ASP 的语法; 5 .使用 Access 数据库编制简单的数据库文件; 6 .能用 FrontPage 和记事本编写 ASP 程序; 7 .能用 Dreamweaver. UltraDev 4.0 和记事本编写 JSP 程序。. 8.1 JavaScript. - PowerPoint PPT Presentation

Transcript of 第 8 章 Web 编程技术

Page 1: 第 8 章  Web 编程技术

第第 88 章 章 WebWeb 编程技术编程技术

Page 2: 第 8 章  Web 编程技术

本章学习目标:本章学习目标: 11 .通过本章的学习,了解.通过本章的学习,了解 JavaScriptJavaScript 脚本的编写脚本的编写方法;方法; 22 .能使用.能使用 JavaScriptJavaScript 编写简单的脚本;编写简单的脚本; 33 .了解什么是.了解什么是 ASPASP 、、 JSPJSP ,它们同,它们同 JavaScriptJavaScript 的的关系如何;关系如何; 44 .理解.理解 ASPASP 的语法;的语法; 55 .使用.使用 AccessAccess 数据库编制简单的数据库文件;数据库编制简单的数据库文件; 66 .能用.能用 FrontPageFrontPage 和记事本编写和记事本编写 ASPASP 程序;程序; 77 .能用.能用 Dreamweaver. UltraDev 4.0Dreamweaver. UltraDev 4.0 和记事本编写和记事本编写

JSPJSP 程序。程序。

Page 3: 第 8 章  Web 编程技术

8.1 JavaScript8.1 JavaScript

Page 4: 第 8 章  Web 编程技术

8.1.1 8.1.1 一个最简单的一个最简单的 JavaScriptJavaScript 脚本脚本 <html><html> <head><head> <title>JavaScript<title>JavaScript 练习练习 </title></title> </head></head> <body><body> </body></body> </html></html>

Page 5: 第 8 章  Web 编程技术

<script language="JavaScript"><script language="JavaScript"> alert ("Warningalert ("Warning ! 我 是! 我 是 JavaScriptJavaScript 初 学初 学者!者! ");"); </script></script>

Page 6: 第 8 章  Web 编程技术

8.1.2 8.1.2 改变背景颜色改变背景颜色 <script language="JavaScript"><script language="JavaScript"> var MyColor;var MyColor; MyColor=prompt("MyColor=prompt(" 您最喜欢的颜色是您最喜欢的颜色是 :","");:",""); switch(MyColor) {switch(MyColor) { case "case " 红色红色 ":": document.bgColor="red";document.bgColor="red"; break;break; case "case " 黄色黄色 ":": document.bgColor="yellow";document.bgColor="yellow";

Page 7: 第 8 章  Web 编程技术

break;break; case "case " 灰色灰色 ":": document.bgColor="gray";document.bgColor="gray"; break;break; default:default: alert("alert(" 我不认识这些颜色我不认识这些颜色 !");!"); }} </script></script>

Page 8: 第 8 章  Web 编程技术

8.1.3 8.1.3 显示日期和时间显示日期和时间 <script language="LiveScript"><script language="LiveScript"> today = new Date()today = new Date() document.write("document.write(" 现在时间是: 现在时间是: ",today.getHours",today.getHours

(),":",today.getMinutes())(),":",today.getMinutes()) document.write("<br>document.write("<br> 今天日期为: 今天日期为: ", today.getM", today.getM

onth()+1,"/",today.getDate()onth()+1,"/",today.getDate() ,"/",today.getYear());,"/",today.getYear()); </script></script>

Page 9: 第 8 章  Web 编程技术

8.1.4 8.1.4 弹出新窗口。弹出新窗口。 <script language="JavaScript"><script language="JavaScript"> window.open("time.htm", "window.open("time.htm", " 日期日期 "); "); </script></script>

Page 10: 第 8 章  Web 编程技术

8.1.5 8.1.5 表单和函数表单和函数 <html><html> <head><head> <script language="LiveScript"><script language="LiveScript"> function WinOpen() {function WinOpen() { window.open("time.htm", "window.open("time.htm", " 日期日期 "); "); }} </script></script> </head></head>

Page 11: 第 8 章  Web 编程技术

<body><body> <form><form> <input type="button" name="Button1" value<input type="button" name="Button1" value

="=" 看看时间看看时间 " onclick="WinOpen()">" onclick="WinOpen()"> </form></form> </body></body> </html></html>

Page 12: 第 8 章  Web 编程技术

8.1.6 8.1.6 用用 JavaScriptJavaScript 编制在线练习编制在线练习题目题目 英语形成性练习题英语形成性练习题

单向选择题,每题单向选择题,每题 2020 分,共分,共 100100 分分 11 、、 You have to ___ your younger brother since mother is not at home. You have to ___ your younger brother since mother is not at home. take care take care take after take after look after look after 22 、、 You have to ___ the registration form in order to use that website. You have to ___ the registration form in order to use that website. carry on carry on fill in fill in keep up keep up 33 、、 David, please ___ these books ___ to the libarary. David, please ___ these books ___ to the libarary. take / back take / back take / off take / off take / out take / out

Page 13: 第 8 章  Web 编程技术

44 、、 If it rains, they will have to ___ the picnic. If it rains, they will have to ___ the picnic. go through go through put off put off set off set off 55 、、 I like ___ the radio. I like ___ the radio. listening at listening at listening for listening for listening tolistening to   

Page 14: 第 8 章  Web 编程技术

<script language="JavaScript"><script language="JavaScript"> function cal(theForm) {function cal(theForm) { var score=0;var score=0; if (theForm.R1.value =="c") score++;if (theForm.R1.value =="c") score++; if (theForm.R2.value =="b") score++;if (theForm.R2.value =="b") score++; if (theForm.R3.value =="a") score++;if (theForm.R3.value =="a") score++; if (theForm.R4.value =="b") score++;if (theForm.R4.value =="b") score++; if (theForm.R5.value =="c") score++;if (theForm.R5.value =="c") score++; alert ("alert (" 你的得分是你的得分是 :"+score*20+":"+score*20+" 分分 ");"); }} </script></script>

Page 15: 第 8 章  Web 编程技术

8.2 ASP8.2 ASP 技术技术

Page 16: 第 8 章  Web 编程技术

8.2.1 ASP8.2.1 ASP 的语法的语法 1、编制的语言1、编制的语言    VBScriptVBScript    JavaScriptJavaScript 2、格式:2、格式: <%<%  开始 开始 %>%>  结束 结束 3、工具:记事本 3、工具:记事本  FrontPage2000/XP DreFrontPage2000/XP Dre

amweave Ultradevamweave Ultradev

Page 17: 第 8 章  Web 编程技术

4. 4. 服务器支持服务器支持 Personal Web ServerPersonal Web Server IISIIS 服务器中必须安装服务器中必须安装 FrontPage FrontPage 服务器扩展支持服务器扩展支持

Page 18: 第 8 章  Web 编程技术

8.2.2 8.2.2 数据库的建立数据库的建立 AccessAccess 数据库的操作和使用数据库的操作和使用

Page 19: 第 8 章  Web 编程技术

8.2.3 8.2.3 在在 ASPASP 中使用中使用 SQLSQL 语法示例语法示例 连接数据库连接数据库 strProvider = "Provider=Microsoft.JET.OLEDB.4.0;strProvider = "Provider=Microsoft.JET.OLEDB.4.0;

Data Source=Data Source= 将该段文字替换为本地硬盘上数据将该段文字替换为本地硬盘上数据库的路径库的路径 ;" ;" Set objConn = server.createobject("ADODB.ConnectSet objConn = server.createobject("ADODB.Connect

ion")ion") objConn.Open strProviderobjConn.Open strProvider

Page 20: 第 8 章  Web 编程技术

DELETEDELETE

strCommand = "DELETE FROM CustomstrCommand = "DELETE FROM Customers WHERE LastName = 'Smith'"ers WHERE LastName = 'Smith'"

objConn.Execute strCommandobjConn.Execute strCommand

Page 21: 第 8 章  Web 编程技术

SELECTSELECT

sql1="select * from online where sql1="select * from online where 姓名姓名 ='" ='" & name & "' and passwd='" & passwd & "'"& name & "' and passwd='" & passwd & "'"

set rs=objConn.execute(sql1)set rs=objConn.execute(sql1)

Page 22: 第 8 章  Web 编程技术

UPDATEUPDATE

strCommand = "UPDATE online SET strCommand = "UPDATE online SET 平平时时 1 = '" & score & "' WHERE 1 = '" & score & "' WHERE 姓名 姓名 = '" = '" & name & "' "& name & "' "

objConn.Execute strCommandobjConn.Execute strCommand

Page 23: 第 8 章  Web 编程技术

INSERTINSERT

strCommand = "INSERT INTO Customers strCommand = "INSERT INTO Customers (FirstName, LastName) VALUES ('Jose','L(FirstName, LastName) VALUES ('Jose','Lugo')" ugo')"

objConn.Execute strCommand objConn.Execute strCommand

Page 24: 第 8 章  Web 编程技术

8.2.4 ASP8.2.4 ASP 中的对象中的对象 11 .. ResponseResponse 该对象用来向文档中输出服务器执行程序该对象用来向文档中输出服务器执行程序的结果 的结果 22 .. RequestRequest 该对象用来获取用户的相关信息 该对象用来获取用户的相关信息

Page 25: 第 8 章  Web 编程技术

33 .. ApplicationApplication ASPASP 中的中的 ApplicationApplication 对象是用来存储各种对象是用来存储各种变量的 变量的 44 .. SessionSession SessionSession 对象也是用来存储各种信息的,但对象也是用来存储各种信息的,但该对象只是针对单一用户而言该对象只是针对单一用户而言

Page 26: 第 8 章  Web 编程技术

55 .. ServerServer 该对象用来获取服务器中的属性和方法 该对象用来获取服务器中的属性和方法 66 .. ErrorError 该对象用来显示一个该对象用来显示一个 ASPASP 页面错误的详细页面错误的详细信息 信息

Page 27: 第 8 章  Web 编程技术

8.2.5 ASP8.2.5 ASP 编程示例编程示例

Page 28: 第 8 章  Web 编程技术

11 、最简单的、最简单的 ASPASP <%@ Language=JScript %> <%@ Language=JScript %> <font face="MS Gothic"> <font face="MS Gothic"> <% <% var strGreeting; var strGreeting; strGreeting = "Hello World!"; strGreeting = "Hello World!"; %> %> <%=strGreeting%> <%=strGreeting%>

Page 29: 第 8 章  Web 编程技术

22 、显示时间、显示时间 <%@ Language=JScript %><%@ Language=JScript %> <% <% var months;var months; var hours;var hours; today = new Date();today = new Date(); months = today.getMonth()+1;months = today.getMonth()+1; hours =today.getHours();hours =today.getHours(); if (hours <12){if (hours <12){ %>%> <%= "<%= " 早上好!早上好! " %>" %>

Page 30: 第 8 章  Web 编程技术

<%<% }else if (hours < 18) {}else if (hours < 18) { %>%> <%= "<%= " 下午好!下午好! "%>"%> <%<% }else if (hours < 24) {}else if (hours < 24) { %>%> <%= "<%= " 晚上好!晚上好! " %>" %> <%<% }} %>%>

Page 31: 第 8 章  Web 编程技术

33 、一个简单的留言本、一个简单的留言本 11 、表单的制作、表单的制作 <form method="POST" name="guestbook" ac<form method="POST" name="guestbook" ac

tion ="gresult.asp">tion ="gresult.asp">

Page 32: 第 8 章  Web 编程技术

2、用2、用 AccessAccess 制作数据库制作数据库 数据库名称: 数据库名称: guestbook.mdbguestbook.mdb 字段:姓名、留言字段:姓名、留言

Page 33: 第 8 章  Web 编程技术

3、数据库操作3、数据库操作

Page 34: 第 8 章  Web 编程技术

4、4、 gresult.aspgresult.asp 代码代码 <%<% dim strTB1, strTB2, strCommand dim strTB1, strTB2, strCommand strTB1 =Server.HTMLEncode(Request.QueryStristrTB1 =Server.HTMLEncode(Request.QueryStri

ng("name")) ng("name")) strTB2 =Server.HTMLEncode(Request.QueryStristrTB2 =Server.HTMLEncode(Request.QueryStri

ng("content"))ng("content")) strProvider = "Provider=Microsoft.JET.OLEDB.4.strProvider = "Provider=Microsoft.JET.OLEDB.4.

0;Data0;Data Source=D:\InetPub\Wwwroot\fpdb\guestbook.mdSource=D:\InetPub\Wwwroot\fpdb\guestbook.md

b;" b;"

Page 35: 第 8 章  Web 编程技术

Set objConn = server.createobject("ADODB.ConnSet objConn = server.createobject("ADODB.Connection")ection")

objConn.Open strProviderobjConn.Open strProvider strCommand = "INSERT INTO guest(strCommand = "INSERT INTO guest( 姓名姓名 ,, 留留言言 ) VALUES ('" ) VALUES ('" strCommand = strCommand & strTB1 & "','" strCommand = strCommand & strTB1 & "','"

& strTB2 & strTB2 strCommand = strCommand & "')" strCommand = strCommand & "')" objConn.Execute strCommandobjConn.Execute strCommand Response.Write("Response.Write(" 谢谢!数据添加成功。谢谢!数据添加成功。 ") ") %> %>

Page 36: 第 8 章  Web 编程技术

8.2.6 8.2.6 在线考试在线考试

Page 37: 第 8 章  Web 编程技术

8.3 JSP8.3 JSP 技术技术 开发环境:开发环境: Java 2+Apache TomcatJava 2+Apache Tomcat

Page 38: 第 8 章  Web 编程技术

8.3.1 JSP8.3.1 JSP 初步初步 11 .一个最简单的.一个最简单的 JSPJSP 程序程序 <%="<%=" 您好!您好! " %> " %>

Page 39: 第 8 章  Web 编程技术

22 .在浏览器中显示日期和时间.在浏览器中显示日期和时间 您好,现在的时间是:您好,现在的时间是: <%= new java.util.<%= new java.util.

Date() %>Date() %>

Page 40: 第 8 章  Web 编程技术

33 .两个页面合并.两个页面合并 <%@ include file="firstjsp.jsp" %><%@ include file="firstjsp.jsp" %>

Page 41: 第 8 章  Web 编程技术

44 .获取客户端浏览器的版本.获取客户端浏览器的版本 您的浏览器版本是:您的浏览器版本是: <%= request.getHeade<%= request.getHeade

r("User-Agent") %>r("User-Agent") %>

Page 42: 第 8 章  Web 编程技术

55 .一个简单的留言本.一个简单的留言本 result.jsp result.jsp <%@ page import = "java.io.*, java.util.*" %><%@ page import = "java.io.*, java.util.*" %> <html><html> <head><head> <title><title> 谢谢留言谢谢留言 </title></title> </head></head> <body><body> 感谢您的留言,按下面链接返回:感谢您的留言,按下面链接返回: <br><a href="guestbook.htm"><br><a href="guestbook.htm"> 返回返回 </a></a> <%<% String yourName = request.getParameter( "T1");String yourName = request.getParameter( "T1"); String yourComment = request.getParameter("S1");String yourComment = request.getParameter("S1"); File file = new File ("D:/Program Files/Apache Tomcat 4.0/webappsFile file = new File ("D:/Program Files/Apache Tomcat 4.0/webapps /ROOT/exercise/mydata.txt");/ROOT/exercise/mydata.txt"); FileWriter fw = new FileWriter(file,true);FileWriter fw = new FileWriter(file,true); PrintWriter pw = new PrintWriter(fw);PrintWriter pw = new PrintWriter(fw); pw.println(yourName + yourComment);pw.println(yourName + yourComment); pw.close();pw.close(); fw.close();fw.close(); %> %> </body></body> </html></html>

Page 43: 第 8 章  Web 编程技术

8.3.2 8.3.2 用用 JSPJSP 开发在线考试程序示开发在线考试程序示例例 11 .数据库的建立.数据库的建立 22 .新站点的定义.新站点的定义 33 .表单的设计.表单的设计 44 .连接数据库.连接数据库 55 .设置登录页面.设置登录页面 66 .创建一个记录.创建一个记录 77 .测试.测试