1 / 15

2.5 服务器端脚本

2.5 服务器端脚本 服务器端的脚本是指能够在服务器上运行的脚本,其脚本形式与客户端的脚本没有太大的差别,也是将脚本直接嵌入到网页中。但不同的是,在网页被传送到浏览器之前,服务器会预先运行网页中的服务器端脚本,运行产生的结果合并到 HTML 中。 与客户端脚本相比优势:( 1 )不存在浏览器兼容的问题,所有浏览器收到的都是标准的 HTML 代码;( 2 )可以很容易地访问服务器上的资源;( 3 )在存取敏感数据时,较为安全,脚本直接在服务器上执行,身份等敏感信息不可能暴露到客户端上;( 4 )简化了客户端的装载,脚本的执行由 Web 服务器承担。

taline
Télécharger la présentation

2.5 服务器端脚本

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. 2.5 服务器端脚本 服务器端的脚本是指能够在服务器上运行的脚本,其脚本形式与客户端的脚本没有太大的差别,也是将脚本直接嵌入到网页中。但不同的是,在网页被传送到浏览器之前,服务器会预先运行网页中的服务器端脚本,运行产生的结果合并到HTML中。 与客户端脚本相比优势:(1)不存在浏览器兼容的问题,所有浏览器收到的都是标准的HTML代码;(2)可以很容易地访问服务器上的资源;(3)在存取敏感数据时,较为安全,脚本直接在服务器上执行,身份等敏感信息不可能暴露到客户端上;(4)简化了客户端的装载,脚本的执行由Web服务器承担。 常用的服务器端的脚本有JSP、PHP、ASP、CGI、Servlet等

  2. 2.5 服务器端脚本 2.5.1 PHP PHP(Personal Hypertext Preprocessor,个人超文本预处理器)是一种易于学习和使用的服务器端的脚本语言,它将自己的标记嵌入在HTML文件中,其语法大部分是从C、JAVA、PERL语言中借用过来的,并形成了自己的独有风格 ; PHP安装方便,简单易学 ; 在PHP中提供了标准的数据库接口,可以连接多种类型的数据库,和自由代码的MYSQL数据库配合更是“天衣无缝” 。 下载:www.php.com

  3. 2.5 服务器端脚本 2.5.1 PHP PHP的安装与设置 安装appserv ( http://www.appserv.com) 安装 apache+php+mysql (http://www.php.com 、 http://www.mysql.com、 http://www.appserv.com) 测试: http://localhost/phpinfo.php <?php phpinfo(); ?>

  4. 2.5 服务器端脚本 2.5.1 PHP 示例1:数据库的生成与数据表的建立 <html> <head> <title>电子商务应用的安装</title> </head> <?php $pagesize = 5; //每页显示5条记录 $host="localhost"; //Mysql数据库管理系统的域名 $user="root"; //Mysql数据库管理系统的登录用户名 $password="111111";//Mysql数据库管理系统的登录用户密码 $dbname="book"; //数据库名称 //登录到MySQL数据库管理系统 mysql_connect("$host","$user","$password") or die("无法连接MySQL数据库服务器!"); mysql_query("drop database if exists ".$dbname); //若book数据库已存在,则删除它 mysql_query("create database ". $dbname); //建立book数据库 $db = mysql_select_db("$dbname") or die("无法连接数据库!"); //打开数据库book echo "Now will insert the database structure<br />"; $structure[] = " CREATE TABLE users ( id int(10) unsigned NOT NULL auto_increment, loginname text NOT NULL, user text binary NOT NULL, password text NOT NULL, department text, title text, role text NOT NULL, approved tinyint(3) unsigned NOT NULL default '0', PRIMARY KEY (id) ) TYPE=MyISAM;"; $structure[] = " CREATE TABLE employees ( id int(10) unsigned NOT NULL auto_increment, no text, user text binary NOT NULL, department text, title text, sex text, birth date, PRIMARY KEY (id) ) TYPE=MyISAM;"; $i = 0; //建立数据表users和employees while($i < count($structure)){ echo "&nbsp;&nbsp;Inserting Table No. ".($i+1); mysql_query("set names GBK"); mysql_query($structure[$i]) or mysql_error(); echo "...Done!<br />"; $i++; } echo "...Done!<br />"; echo "set users initial value...<br />"; mysql_query("set names GBK"); mysql_query("INSERT INTO users SET id=1,loginname='admin',user='admin',password='123456',role='1',approved=1"); echo "...Done!<br />"; ?> </html>

  5. 2.5 服务器端脚本 2.5.1 PHP 示例2:利用PHP脚本实现文件的上传 步骤1:建立网页文件upload.htm,存放到站点根目录下 ; 步骤2:建立脚本文件upload.php,存放到站点根目录下 ; 运行 <html> <head> <title>上传文件</title> </head> <body> <form target="_self" enctype="multipart/form-data" action="upload.php" method="post"> <table border="0" width="80%" cellspacing="0" cellpadding="0" align="center"> <tr><td aligh="left">请指定文件: <input type="hidden" name="max_file_size" value="8000000"></td></tr> <tr><td><input name="upfile[]" type="file" size=40></td></tr> <tr><td><input name="upfile[]" type="file" size=40></td></tr> <tr><td><input name="upfile[]" type="file" size=40></td></tr> <tr><td><input name="upfile[]" type="file" size=40></td></tr> <tr><td>&nbsp;</td></tr> <tr><td><input type="submit" value="上传文件">&nbsp;&nbsp;&nbsp;&nbsp;<input type='button' value='关闭窗口' onclick='window.close()' /></td></tr> <tr><td>&nbsp;</td></tr> <tr><td>&nbsp;</td></tr> <tr><td><font color='blue' size="1">说明:一次最多上传四个文件,每个文件大小应小于8M字节</font></td></tr> </table> </form> </body> </html> <?php $success=true; $filecount=0; $filename=""; $failurefilename=""; $dir="c:/".substr(date("Y-m-d"),0,4); if(!is_dir($dir)) mkdir($dir); for($i=0;$i<4;$i++) { if($_FILES['upfile']['tmp_name'][$i] != "") { $result=move_uploaded_file($_FILES['upfile']['tmp_name'][$i],$dir."/".$_FILES['upfile']['name'][$i]); if ($result){ $filecount=$filecount+1; $filename.=$_FILES['upfile']['name'][$i].","; } else { $success=false; $failurefilename.=$_FILES['upfile']['name'][$i].","; } } } //end for if($success && $filecount>0) { ?> <html> <head> <title>上传文件结果</title> </head> <body > <table border="0" width="80%" cellspacing="0" cellpadding="0" > <tr><td>文件:<?=$filename?>已成功上传,共计<?=$filecount?>个文件</td></tr> <tr><td>&nbsp;</td></tr> <tr><td>&nbsp;</td></tr> <tr><td align="center"><input type='button' value='关闭窗口' onclick='window.close()' /></td> </tr> </table> </body> </html> <?php exit; } else { ?> <html> <head> <title>上传图像文件</title> </head> <body> <?php if($filecount>0) echo "<font style='font-color:red'>文件:".$failurefilename."上传失败,请重新上传</font><br>"; else echo "<font style='font-color:red'>文件上传失败,请重新上传</font><br>"; ?> </body> </html> <?php } ?>

  6. 2.5 服务器端脚本 2.5.2 ASP ASP(Active Service Page,活动服务页),是微软开发的一种服务器端的脚本技术,它没有提供自己专门的编程语言,而是允许用户使用包括VBScript、JavaScript等在内的许多已有的脚本语言来编写ASP的应用程序。ASP吸收了当今许多流行的技术,如IIS、ActiveX、VBScript、ODBC等,是一种发展较为成熟的Web应用程序开发技术;其核心功能支持对象组件,通过使用ASP的组件和对象,调用ActiveX控件,可实现强大的商务功能 。 常用的内置对象和组件主要有: (1)Request对象:用来连接客户端的Web页(.htm文件)和服务器的Web页(.asp文件); (2)Response对象:用于将服务端数据发送到客户端; (3)Server对象:它可以创建各种Server对象的实例以简化用户的操作; (4)Application对象:它是应用程序级的对象,用来在所有用户间共享信息,所有客户都可以访问这个对象。 (5)Session对象:它为每个访问者提供一个标识;Session可以用来存储访问者的一些数据,在购物网站中,Session常用于创建购物车(Shopping Cart)。

  7. 2.5 服务器端脚本 2.5.2 ASP 示例1:查询书籍 (search.asp) <html> <head> <title>图书目录</title> </head> <body> <form name="form1" method="post" action="search.asp" > 查找图书:<input type="text" name="t0"><br> <input type="submit"> </form> <% x=Request("t0") if x<>"" then set book_db=server.createobject("adodb.connection") book_db.open "DSN=book" '数据源名字为book set rsbookrec=server.createobject("ADODB.recordset") y="SELECT * FROM tab1 where book_name like '%" & x & "%'" 'tab1为表的名字 rsbookrec.open y,book_db do while not rsbookrec.EOF %> <p><td><%=rsbookrec("book_name") %></td> <td><%=rsbookrec("Author")%></td> <% rsbookrec.movenext loop book_db.close set book_db=nothing end if %> </body> </html>

  8. 2.5 服务器端脚本 2.5.2 ASP 说明 运行search.asp时,需要先在Access中创建一个数据库文件book.mdb,它具有一个数据表tab1,内含字段Book_name、author;同时需要在“控制面板”窗口中建立一个名为book的数据源,该数据源对应数据库book.mdb。将search.asp拷贝到IIS服务器的根目录inetpub\wwwroot下,在浏览器的地址栏中输入http://127.0.0.1/book.asp即可进行测试

  9. 2.5 服务器端脚本 2.5.3 JSP JSP(Java Service Pages)是由Sun公司于1999年6月推出的基于Java Servlet以及整个java体系的Web开发技术。利用这一技术可以建立先进、安全和跨平台的动态网站 ; JSP和ASP有许多相似之处。两者都提供动态网页的技术运行环境,都能实现网页与组件的分离,都能替代CGI,使得动态网页的制作变得简单与快捷。但两者来源于不同的技术规范与组织,相比只能运行于Windows平台下的ASP,JSP可以不加修改地运行在大都数的服务器平台上,这其中也包括Windows系统,符合“write once,run anywhere”(一次编写,多平台运行)的java标准,能很好地实现平台和服务器的独立性。 JSP代码的执行,需要在Web服务器上安装JSP引擎,可从www.java.sun.com下载JSWDK(Java Server Web Development Kit)并安装,安装之后只需执行startserver命令即可启动JSP服务,输入http://localhost:8080可打开默认页面。当然,有的应用服务器直接支持JSP,如WebLogic Application Server,Apache Tomcat,Allarie ,Jrun等 。

  10. 2.5 服务器端脚本 2.5.3 JSP 示例1 <%@page language="java" contentType="text/html;charset=gbk"%> <HTML> <head > <title>Hello World!</title> </head> <body bgcolor="#FFFFFF"> <%!String msg="一个简单的JSP";%> <% out.println("Hello World!"); out.println("Hello China!"); out.println(msg); %> <br> <%=msg%> <%--显示变量值--%> </body> </HTML>

  11. 2.5 服务器端脚本 2.5.3 JSP JSP的基本语法与内部对象

  12. 2.5 服务器端脚本 2.5.3 JSP 示例2: <html> <head><title>get request info example </title> </head> <body> <form action="req1.jsp"> <br>Get request results. <br><input type="text" name="myname"> <br><input type="submit" name="get value"> </form> Request Method:<%=request.getMethod()%> <br>Servlet Path:<%=request.getServletPath()%> <br>Query String:<%=request.getQueryString()%> <br>Content Type:<%=request.getContentType()%> <br>Protocol:<%=request.getProtocol()%> <br>Path Info:<%=request.getPathInfo()%> <br>Server Name:<%=request.getServerName()%> <br>Server Port:<%=request.getServerPort()%> <br>Remote Host:<%=request.getRemoteHost()%> <br>Remote Addr:<%=request.getRemoteAddr()%> <br>Scheme:<%=request.getScheme()%> <br>Get request value:<%=request.getParameter("myname")%> </body> </html>

  13. 2.5 服务器端脚本 2.5.3 JSP 示例4:利用JSP实现登录功能 主页: <html> <head> <title>connect the db</title> </head> <body bgcolor="#ffffff"> <form action="reg.jsp" method="post"> <input style="font-size:12px;width:210px" type="text" name="user" size="20"> <input style="font-size:12px;width:210px" type="password" name="password" size="20"> <br> <input type="submit" name="user" value="登录"> <input type="reset" name="user" value="取消"> </form> </body> </html> 登录页reg.jsp: <%@ page contentType="text/html; charset=gb2312" %> <html> <head> <title>connect the db</title> </head> <%@ page import="java.sql.*"%> <body bgcolor="#ffffff"> <% Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); String url="jdbc:odbc:ec"; String user=request.getParameter("user"); String password=request.getParameter("password"); Connection conn= DriverManager.getConnection(url,user,password); Statement stmt=conn.createStatement(); String sql="select * from member where user_name='"+user+"'"; //member为数据源ec库中的一张表; ResultSet rs=stmt.executeQuery(sql); if(rs.next()) { out.print("你已成功登录!"); out.print("你的身份是:"+rs.getString("user_name")); //response.sendRedirect("index_buy.jsp"); } else { out.print("你还没有注册,请先注册!"); } rs.close(); stmt.close(); conn.close(); %> </body> </html>

  14. 2.5 服务器端脚本 2.5.3 JSP 示例5:利用JSP实现“音乐无国界”在线商店

  15. 习题与思考 商务表达层的功能是什么?其平台实现方式有哪几种? 静态网页的特征是什么?其体系结构如何? HTTP的含义是什么?有什么作用?具有哪些命令及命令格式? 试通过网络,详细了解HTTP协议的内容,并说明几种状态码的含义。 标记语言的种类有哪些? XML语言有何作用?在XML中为何要使用名称空间? CSS和XSL文件的作用是什么?试举例说明。 XML文档的应用开发涉及哪几个方面?试举例说明。 XSL解析器如何解析XML文档?试举例说明。 构成Web系统的五大要素分别是什么? 试用Javascript编写一个简单的交互应用网页。 试用Applet编写一个简单的动态网页。 试用ASP或PHP编写一个简单的Web应用程序,并将它布置到服务器中去。 试用JSP编写一个简单的学生成绩管理系统。 试用JSP编写一个简单的网上书店系统, 并将它布置到TOMCAT服务器中去

More Related