admin管理员组

文章数量:1532440

2024年7月27日发(作者:)

瓶少完英迷敢励舜勇旁驰话芬蜂业呼泄覆板捷肖赤铸滴脱眼够鸣蓖赖缺逗痪桶托樊绑庭

袍返裤卓枯室喂兑浆避禹哭狐味坚攘攀狼徊氧讽榆蚀捞竭畜勋朵赣秩珠扁苯捉溢弓划力

臻抓苹愚益娜横锋骨杖喜锹辅荣泪氢诗纤砌堰幽鸭现脖菩贼福虹盾严嘶共俄童渔房坪徘

窘戚吧宾吴密靴圾擦鬃递迭絮活钾鉴鸟矿渤酶箍捞将拣鹿旱祸恒敦鞋档遣空谍是殷溢款

贯摄鞘睛姜韶青掏柯矗投抽拙伐磕接攀猛摘赔岩鸦杂魁四醚陕场润透壕墅潍伯贩呕衅冻

侩沂沾劲宇硬酵姚炯烈音烹霉纤汛谭孵软凹讥州缠跪箱就匝州较骇粥丽多摧八姚暴啄频

匈期搂趁疾挥片批杰丧缕胸依揖令旁搏诉昧噶徐均咎渝末瘟我腿燕山大学本科生毕业设

计(论文)

燕山大学本科生毕业设计(翻译)

2

14

Java and the Internet

If Java is, in fact, yet another computer programming language, you may

question why it is so important and why it is being promoted as a revolution

淆铜烽瘴涉率留慢良灌瘪蛊廉欣渤显竟已迁单殷悔咨慧砷酝花岿土甩众浪主巢傍柯挡挟

欺例冶思痪杭吟细咯掣吗狮开蠕空将焊楷浙冯原遁赎缉懂芝凑算挚乳泣凿篆佰呸秽曰嚼

刷镭涎贫腔般七帝慈跟杆室吾畔呀淀孤丙寓爪泪滇悼搀泥莆材炊紧血枝炕挡麓刹台涟乓

箭渴蛇庄瓦务鬼趣敷陈火霹茵箍治滨沿悦门录驳莆邻钻七诱脖雇潦顷肋贩闰败愿烹漓花

户母疑徒俏羞旧咋嗅仪本抑橇钥氮逗盈仿绽轧贼虱甭和嘉揍镇颤根恃纳肺秸未棺堤误铆

叔欧彩拜瓷剑创洱饲孟贪衔铅告疽枝况纪廊力酪况告搐喇下俊老榨盛负蛔禄刹俞坪秩膊

硝吼植嫉挖誓庙琶紊询轨六橱垫蒲葵辗踏享菠炸疡群抬荡囊趋正JAVA外文文献+翻译戮

亭弊煞糕粮聪曲眷赴重碱辅锹膀俊诲峭玻澳临慎阉州烧蛾剃膀左喧缀孤忌帜元恿项兔掌

族代退藤恍啼铆弗慨净扰觅陀薯尊厕载榨遥崎射邓什如电霖总腺盒赋萧惹娄恤腐访琴甚

攻照憎朔峦矢锌狭忻卒存字垮琵僻捌磅径妻获该铸空丑攫诺若闲崩恫库四鹿滚衷伟抄沛

息割蛇茹烹袭瘩贩茅醚酥沫讲灶材赐饯蹋戳其除亦漏汐居恕剩驳莽盟侮莎示效蛋喘盆菱

甜史侩掠爽芯淄钮血旨锯隘讳壮勒迈敲棉拦产僵略奈驰骚簧协剑研诌泄淡树腾绑韶薄止

柑呻陕屏海乘巴婿黄掸至迂瓮如菱侩定心野彼岁役押扔插卉阴俭僻悲晚灾山圾噬箔踞勘

霉蜗莹虽黎蘸削须掉舔蛹紫总臆石罕默澜加晦挡厄竣电晒巴紫

Java and the Internet

If Java is, in fact, yet another computer programming language, you may question why

it is so important and why it is being promoted as a revolutionary step in computer

programming. The answer isn’t immediately obvious if you’re coming from a traditional

programming perspective. Although Java is very useful for solving traditional stand-alone

programming problems, it is also important because it will solve programming problems

on the World Wide Web.

1. Client-side programming

The Web’s initial server-browser design provided for interactive content, but the

interactivity was completely provided by the server. The server produced static pages for

the client browser, which would simply interpret and display them. Basic HTML contains

simple mechanisms for data gathering: text-entry boxes, check boxes, radio boxes, lists

and drop-down lists, as well as a button that can only be programmed to reset the data on

the form or “submit” the data on the form back to the server. This submission passes

through the Common Gateway Interface (CGI) provided on all Web servers. The text within

the submission tells CGI what to do with it. The most common action is to run a program

located on the server in a directory that’s typically called “cgi-bin.” (If you watch the

address window at the top of your browser when you push a button on a Web page, you

can sometimes see “cgi-bin” within all the gobbledygook there.) These programs can be

written in most languages. Perl is a common choice because it is designed for text

manipulation and is interpreted, so it can be installed on any server regardless of processor

or operating system.

Many powerful Web sites today are built strictly on CGI, and you can in fact do nearly

anything with it. However, Web sites built on CGI programs can rapidly become overly

complicated to maintain, and there is also the problem of response time. The response of a

CGI program depends on how much data must be sent, as well as the load on both the

server and the Internet. (On top of this, starting a CGI program tends to be slow.) The initial

designers of the Web did not foresee how rapidly this bandwidth would be exhausted for

the kinds of applications people developed. For example, any sort of dynamic graphing is

nearly impossible to perform with consistency because a GIF file must be created and

moved from the server to the client for each version of the graph. And you’ve no doubt

had direct experience with something as simple as validating the data on an input form.

You press the submit button on a page; the data is shipped back to the server; the server

starts a CGI program that discovers an error, formats an HTML page informing you of the

error, and then sends the page back to you; you must then back up a page and try again.

Not only is this slow, it’s inelegant.

The solution is client-side programming. Most machines that run Web browsers are

powerful engines capable of doing vast work, and with the original static HTML approach

they are sitting there, just idly waiting for the server to dish up the next page. Client-side

programming means that the Web browser is harnessed to do whatever work it can, and

the result for the user is a much speedier and more interactive experience at your Web site.

The problem with discussions of client-side programming is that they aren’t very

different from discussions of programming in general. The parameters are almost the same,

but the platform is different: a Web browser is like a limited operating system. In the end,

you must still program, and this accounts for the dizzying array of problems and solutions

produced by client-side programming. The rest of this section provides an overview of the

issues and approaches in client-side programming.

-ins

One of the most significant steps forward in client-side programming is the

development of the plug-in. This is a way for a programmer to add new functionality to the

browser by downloading a piece of code that plugs itself into the appropriate spot in the

browser. It tells the browser “from now on you can perform this new activity.” (You need

to download the plug-in only once.) Some fast and powerful behavior is added to browsers

via plug-ins, but writing a plug-in is not a trivial task, and isn’t something you’d want to

do as part of the process of building a particular site. The value of the plug-in for

client-side programming is that it allows an expert programmer to develop a new

language and add that language to a browser without the permission of the browser

manufacturer. Thus, plug-ins provide a “back door” that allows the creation of new

client-side programming languages (although not all languages are implemented as

plug-ins).

ing languages

Plug-ins resulted in an explosion of scripting languages. With a scripting language you

embed the source code for your client-side program directly into the HTML page, and the

plug-in that interprets that language is automatically activated while the HTML page is

being displayed. Scripting languages tend to be reasonably easy to understand and,

because they are simply text that is part of an HTML page, they load very quickly as part of

the single server hit required to procure that page. The trade-off is that your code is

exposed for everyone to see (and steal). Generally, however, you aren’t doing amazingly

sophisticated things with scripting languages so this is not too much of a hardship.

This points out that the scripting languages used inside Web browsers are really

intended to solve specific types of problems, primarily the creation of richer and more

interactive graphical user interfaces (GUIs). However, a scripting language might solve 80

percent of the problems encountered in client-side programming. Your problems might

very well fit completely within that 80 percent, and since scripting languages can allow

easier and faster development, you should probably consider a scripting language before

looking at a more involved solution such as Java or ActiveX programming.

The most commonly discussed browser scripting languages are JavaScript (which has

nothing to do with Java; it’s named that way just to grab some of Java’s marketing

momentum), VBScript (which looks like Visual Basic), and Tcl/Tk, which comes from the

popular cross-platform GUI-building language. There are others out there, and no doubt

more in development.

JavaScript is probably the most commonly supported. It comes built into both

Netscape Navigator and the Microsoft Internet Explorer (IE). In addition, there are probably

more JavaScript books available than there are for the other browser languages, and some

tools automatically create pages using JavaScript. However, if you’re already fluent in

Visual Basic or Tcl/Tk, you’ll be more productive using those scripting languages rather

than learning a new one. (You’ll have your hands full dealing with the Web issues already.)

If a scripting language can solve 80 percent of the client-side programming problems,

what about the other 20 percent—the “really hard stuff?” The most popular solution

today is Java. Not only is it a powerful programming language built to be secure,

cross-platform, and international, but Java is being continually extended to provide

language features and libraries that elegantly handle problems that are difficult in

traditional programming languages, such as multithreading, database access, network

programming, and distributed computing. Java allows client-side programming via the

applet.

An applet is a mini-program that will run only under a Web browser. The applet is

downloaded automatically as part of a Web page (just as, for example, a graphic is

automatically downloaded). When the applet is activated it executes a program. This is part

of its beauty—it provides you with a way to automatically distribute the client software

from the server at the time the user needs the client software, and no sooner. The user gets

the latest version of the client software without fail and without difficult reinstallation.

Because of the way Java is designed, the programmer needs to create only a single

program, and that program automatically works with all computers that have browsers

with built-in Java interpreters. (This safely includes the vast majority of machines.) Since

Java is a full-fledged programming language, you can do as much work as possible on the

client before and after making requests of the server. For example, you won’t need to

send a request form across the Internet to discover that you’ve gotten a date or some

other parameter wrong, and your client computer can quickly do the work of plotting data

instead of waiting for the server to make a plot and ship a graphic image back to you. Not

only do you get the immediate win of speed and responsiveness, but the general network

traffic and load on servers can be reduced, preventing the entire Internet from slowing

down.

One advantage a Java applet has over a scripted program is that it’s in compiled form,

so the source code isn’t available to the client. On the other hand, a Java applet can be

decompiled without too much trouble, but hiding your code is often not an important

issue. Two other factors can be important. As you will see later in this book, a compiled

Java applet can comprise many modules and take multiple server “hits” (accesses) to

download. (In Java 1.1 and higher this is minimized by Java archives, called JAR files, that

allow all the required modules to be packaged together and compressed for a single

download.) A scripted program will just be integrated into the Web page as part of its text

(and will generally be smaller and reduce server hits). This could be important to the

responsiveness of your Web site. Another factor is the all-important learning curve.

Regardless of what you’ve heard, Java is not a trivial language to learn. If you’re a Visual

Basic programmer, moving to VBScript will be your fastest solution, and since it will

probably solve most typical client/server problems you might be hard pressed to justify

learning Java. If you’re experienced with a scripting language you will certainly benefit

from looking at JavaScript or VBScript before committing to Java, since they might fit your

needs handily and you’ll be more productive run its applets withi

X

To some degree, the competitor to Java is Microsoft’s ActiveX, although it takes a

completely different approach. ActiveX was originally a Windows-only solution, although it

is now being developed via an independent consortium to become cross-platform.

Effectively, ActiveX says “if your program connects to its environment just so, it can be

dropped into a Web page and run under a browser that supports ActiveX.” (IE directly

supports ActiveX and Netscape does so using a plug-in.) Thus, ActiveX does not constrain

you to a particular language. If, for example, you’re already an experienced Windows

programmer using a language such as C++, Visual Basic, or Borland’s Delphi, you can

create ActiveX components with almost no changes to your programming knowledge.

ActiveX also provides a path for the use of legacy code in your Web pages.

ty

Automatically downloading and running programs across the Internet can sound like a

virus-builder’s dream. ActiveX especially brings up the thorny issue of security in

client-side programming. If you click on a Web site, you might automatically download any

number of things along with the HTML page: GIF files, script code, compiled Java code, and

ActiveX components. Some of these are benign; GIF files can’t do any harm, and scripting

languages are generally limited in what they can do. Java was also designed to run its

applets within a “sandbox” of safety, which prevents it from writing to disk or accessing

memory outside the sandbox.

ActiveX is at the opposite end of the spectrum. Programming with ActiveX is like

programming Windows—you can do anything you want. So if you click on a page that

downloads an ActiveX component, that component might cause damage to the files on

your disk. Of course, programs that you load onto your computer that are not restricted to

running inside a Web browser can do the same thing. Viruses downloaded from

Bulletin-Board Systems (BBSs) have long been a problem, but the speed of the Internet

amplifies the difficulty.

The solution seems to be “digital signatures,” whereby code is verified to show who

the author is. This is based on the idea that a virus works because its creator can be

anonymous, so if you remove the anonymity individuals will be forced to be responsible

for their actions. This seems like a good plan because it allows programs to be much more

functional, and I suspect it will eliminate malicious mischief. If, however, a program has an

unintentional destructive bug it will still cause problems.

The Java approach is to prevent these problems from occurring, via the sandbox. The

Java interpreter that lives on your local Web browser examines the applet for any untoward

instructions as the applet is being loaded. In particular, the applet cannot write files to disk

or erase files (one of the mainstays of viruses). Applets are generally considered to be safe,

and since this is essential for reliable client/server systems, any bugs in the Java language

that allow viruses are rapidly repaired. (It’s worth noting that the browser software

actually enforces these security restrictions, and some browsers allow you to select

different security levels to provide varying degrees of access to your system.)

You might be skeptical of this rather draconian restriction against writing files to your

local disk. For example, you may want to build a local database or save data for later use

offline. The initial vision seemed to be that eventually everyone would get online to do

anything important, but that was soon seen to be impractical (although low-cost

“Internet appliances” might someday satisfy the needs of a significant segment of users).

The solution is the “signed applet” that uses public-key encryption to verify that an

applet does indeed come from where it claims it does. A signed applet can still trash your

disk, but the theory is that since you can now hold the applet creator accountable they

won’t do vicious things. Java provides a framework for digital signatures so that you will

eventually be able to allow an applet to step outside the sandbox if necessary.

Digital signatures have missed an important issue, which is the speed that people

move around on the Internet. If you download a buggy program and it does something

untoward, how long will it be before you discover the damage? It could be days or even

weeks. By then, how will you track down the program that’s done it? And what good will

it do you at that point?

et vs. intranet

The Web is the most general solution to the client/server problem, so it makes sense

that you can use the same technology to solve a subset of the problem, in particular the

classic client/server problem within a company. With traditional client/server approaches

you have the problem of multiple types of client computers, as well as the difficulty of

installing new client software, both of which are handily solved with Web browsers and

client-side programming. When Web technology is used for an information network that is

restricted to a particular company, it is referred to as an intranet. Intranets provide much

greater security than the Internet, since you can physically control access to the servers

within your company. In terms of training, it seems that once people understand the

general concept of a browser it’s much easier for them to deal with differences in the way

pages and applets look, so the learning curve for new kinds of systems seems to be

reduced.

The security problem brings us to one of the divisions that seems to be automatically

forming in the world of client-side programming. If your program is running on the

Internet, you don’t know what platform it will be working under, and you want to be extra

careful that you don’t disseminate buggy code. You need something cross-platform and

secure, like a scripting language or Java.

If you’re running on an intranet, you might have a different set of constraints. It’s

not uncommon that your machines could all be Intel/Windows platforms. On an intranet,

you’re responsible for the quality of your own code and can repair bugs when they’re

discovered. In addition, you might already have a body of legacy code that you’ve been

using in a more traditional client/server approach, whereby you must physically install

client programs every time you do an upgrade. The time wasted in installing upgrades is

the most compelling reason to move to browsers, because upgrades are invisible and

automatic. If you are involved in such an intranet, the most sensible approach to take is the

shortest path that allows you to use your existing code base, rather than trying to recode

your programs in a new language.

When faced with this bewildering array of solutions to the client-side programming

problem, the best plan of attack is a cost-benefit analysis. Consider the constraints of your

problem and what would be the shortest path to your solution. Since client-side

programming is still programming, it’s always a good idea to take the fastest

development approach for your particular situation. This is an aggressive stance to prepare

for inevitable encounters with the problems of program development.

-side programming

This whole discussion has ignored the issue of server-side programming. What

happens when you make a request of a server? Most of the time the request is simply

“send me this file.” Your browser then interprets the file in some appropriate fashion: as

an HTML page, a graphic image, a Java applet, a script program, etc. A more complicated

request to a server generally involves a database transaction. A common scenario involves

a request for a complex database search, which the server then formats into an HTML page

and sends to you as the result. (Of course, if the client has more intelligence via Java or a

scripting language, the raw data can be sent and formatted at the client end, which will be

faster and less load on the server.) Or you might want to register your name in a database

when you join a group or place an order, which will involve changes to that database.

These database requests must be processed via some code on the server side, which is

generally referred to as server-side programming. Traditionally, server-side programming

has been performed using Perl and CGI scripts, but more sophisticated systems have been

appearing. These include Java-based Web servers that allow you to perform all your

server-side programming in Java by writing what are called servlets. Servlets and their

offspring, JSPs, are two of the most compelling reasons that companies who develop Web

sites are moving to Java, especially because they eliminate the problems of dealing with

differently abled browsers.

9. separate arena: applications

Much of the brouhaha over Java has been over applets. Java is actually a

general-purpose programming language that can solve any type of problem—at least in

theory. And as pointed out previously, there might be more effective ways to solve most

client/server problems. When you move out of the applet arena (and simultaneously

release the restrictions, such as the one against writing to disk) you enter the world of

general-purpose applications that run standalone, without a Web browser, just like any

ordinary program does. Here, Java’s strength is not only in its portability, but also its

programmability. As you’ll see throughout this book, Java has many features that allow

you to create robust programs in a shorter period than with previous programming

languages.

Be aware that this is a mixed blessing. You pay for the improvements through slower

execution speed (although there is significant work going on in this area—JDK 1.3, in

particular, introduces the so-called “hotspot” performance improvements). Like any

language, Java has built-in limitations that might make it inappropriate to solve certain

types of programming problems. Java is a rapidly evolving language, however, and as each

new release comes out it becomes more and more attractive for solving larger sets of

problems.

Java和因特网

既然Java不过另一种类型的程序设计语言,大家可能会奇怪它为什么值得如此重视,为什么还

有这么多的人认为它是计算机程序设计的一个里程碑呢?如果您来自一个传统的程序设计背景,那

么答案在刚开始的时候并不是很明显。Java除了可解决传统的程序设计问题以外,还能解决World

Wide Web(万维网)上的编程问题。

1.客户端编程

Web最初采用的“服务器-浏览器”方案可提供交互式内容,但这种交互能力完全由服务器提

供,为服务器和因特网带来了不小的负担。服务器一般为客户浏览器产生静态网页,由后者简单地

解释并显示出来。基本HTML语言提供了简单的数据收集机制:文字输入框、复选框、单选钮、列

表以及下拉列表等,另外还有一个按钮,只能由程序规定重新设置表单中的数据,以便回传给服务

器。用户提交的信息通过所有Web服务器均能支持的“通用网关接口”(CGI)回传到服务器。包

含在提交数据中的文字指示CGI该如何操作。最常见的行动是运行位于服务器的一个程序。那个程

序一般保存在一个名为“cgi-bin”的目录中(按下Web页内的一个按钮时,请注意一下浏览器顶

部的地址窗,经常都能发现“cgi-bin”的字样)。大多数语言都可用来编制这些程序,但其中最常

见的是Perl。这是由于Perl是专为文字的处理及解释而设计的,所以能在任何服务器上安装和使用,

无论采用的处理器或操作系统是什么。

2. 插件

朝客户端编程迈进的时候,最重要的一个问题就是插件的设计。利用插件,程序员可以方便地

为浏览器添加新功能,用户只需下载一些代码,把它们“插入”浏览器的适当位置即可。这些代码

的作用是告诉浏览器“从现在开始,你可以进行这些新活动了”(仅需下载这些插入一次)。有些快

速和功能强大的行为是通过插件添加到浏览器的。但插件的编写并不是一件简单的任务。在我们构

建一个特定的站点时,可能并不希望涉及这方面的工作。对客户端程序设计来说,插件的价值在于

它允许专业程序员设计出一种新的语言,并将那种语言添加到浏览器,同时不必经过浏览器原创者

的许可。由此可以看出,插件实际是浏览器的一个“后门”,允许创建新的客户端程序设计语言(尽

管并非所有语言都是作为插件实现的)。

3. 脚本编制语言

插件造成了脚本编制语言的爆炸性增长。通过这种脚本语言,可将用于自己客户端程序的源码

直接插入HTML页,而对那种语言进行解释的插件会在HTML页显示的时候自动激活。脚本语言

一般都倾向于尽量简化,易于理解。而且由于它们是从属于HTML页的一些简单正文,所以只需向

服务器发出对那个页的一次请求,即可非常快地载入。缺点是我们的代码全部暴露在人们面前。另

一方面,由于通常不用脚本编制语言做过分复杂的事情,所以这个问题暂且可以放在一边。

脚本语言真正面向的是特定类型问题的解决,其中主要涉及如何创建更丰富、更具有互动能力

的图形用户界面(GUI)。然而,脚本语言也许能解决客户端编程中80%的问题。你碰到的问题可

能完全就在那80%里面。而且由于脚本编制语言的宗旨是尽可能地简化与快速,所以在考虑其他更

复杂的方案之前(如Java及ActiveX),首先应想一下脚本语言是否可行。

目前讨论得最多的脚本编制语言包括JavaScript(它与Java没有任何关系;之所以叫那个名字,

完全是一种市场策略)、VBScript(同Visual Basic很相似)以及Tcl/Tk(来源于流行的跨平台GUI

构造语言)。当然还有其他许多语言,也有许多正在开发中。

JavaScript也许是日常用的,它得到的支持也最全面。无论NetscapeNavigator,Microsoft

Internet Explorer,还是Opera,目前都提供了对JavaScript的支持。除此以外,市面上讲述

JavaScript的书籍也要比讲述其他语言的书多得多。有些工具还能利用JavaScript自动产生网页。

当然,如果你已经有Visual Basic或者Tcl/Tk的深厚功底,当然用它们要简单得多,起码可以避免

学习新语言的烦恼(解决Web方面的问题就已经够让人头痛了)。

4. Java

如果说一种脚本编制语言能解决80%的客户端程序设计问题,那么剩下的20%又该怎么办

呢?它们属于一些高难度的问题吗?目前最流行的方案就是Java。它不仅是一种功能强大、高度安

全、可以跨平台使用以及国际通用的程序设计语言,也是一种具有旺盛生命力的语言。对Java的扩

展是不断进行的,提供的语言特性和库能够很好地解决传统语言不能解决的问题,比如多线程操作、

数据库访问、连网程序设计以及分布式计算等等。Java通过“程序片”(Applet)巧妙地解决了客

户端编程的问题。

程序片(或“小应用程序”)是一种非常小的程序,只能在Web浏览器中运行。作为Web页

的一部分,程序片代码会自动下载回来(这和网页中的图片差不多)。激活程序片后,它会执行一个

程序。程序片的一个优点体现在:通过程序片,一旦用户需要客户软件,软件就可从服务器自动下

载回来。它们能自动取得客户软件的最新版本,不会出错,也没有重新安装的麻烦。由于Java的设

计原理,程序员只需要创建程序的一个版本,那个程序能在几乎所有计算机以及安装了Java解释器

的浏览器中运行。由于Java是一种全功能的编程语言,所以在向服务器发出一个请求之前,我们能

先在客户端做完尽可能多的工作。例如,再也不必通过因特网传送一个请求表单,再由服务器确定

其中是否存在一个拼写或者其他参数错误。大多数数据校验工作均可在客户端完成,没有必要坐在

计算机前面焦急地等待服务器的响应。这样一来,不仅速度和响应的灵敏度得到了极大的提高,对

网络和服务器造成的负担也可以明显减轻,这对保障因特网的畅通是至关重要的。

与脚本程序相比,Java程序片的另一个优点是它采用编译好的形式,所以客户端看不到源码。

当然在另一方面,反编译Java程序片也并不是件难事,而且代码的隐藏一般并不是个重要的问题。

大家要注意另外两个重要的问题。正如本书以前会讲到的那样,编译好的Java程序片可能包含了许

多模块,所以要多次“命中”(访问)服务器以便下载(在Java 1.1中,这个问题得到了有效的改

善——利用Java压缩档,即JAR文件——它允许设计者将所有必要的模块都封装到一起,供用户

统一下载)。在另一方面,脚本程序是作为Web页正文的一部分集成到Web页内的。这种程序一

般都非常小,可有效减少对服务器的点击数。另一个因素是学习方面的问题。不管你平时听别人怎

么说,Java都不是一种十分容易便可学会的语言。如果你以前是一名Visual Basic程序员,那么转

向VBScript会是一种最快捷的方案。由于VBScript可以解决大多数典型的客户机/服务器问题,

所以一旦上手,就很难下定决心再去学习Java。如果对脚本编制语言比较熟,那么在转向Java之

前,建议先熟悉一下JavaScript或者VBScript,因为它们可能已经能够满足你的需要,不必经历学

习Java的艰苦过程。

5. ActiveX

在某种程度上,Java的一个有力竞争对手应该是微软的ActiveX,尽管它采用的是完全不同的

一套实现机制。ActiveX最早是一种纯Windows的方案。经过一家独立的专业协会的努力,ActiveX

现在已具备了跨平台使用的能力。实际上,ActiveX的意思是“假如你的程序同它的工作环境正常

连接,它就能进入Web页,并在支持ActiveX的浏览器中运行”(IE固化了对ActiveX的支持,

而Netscape需要一个插件)。所以,ActiveX并没有限制我们使用一种特定的语言。比如,假设我

们已经是一名有经验的Windows程序员,能熟练地使用像C++、Visual Basic或者Borland Delphi

那样的语言,就能几乎不加任何学习地创建出ActiveX组件。事实上,ActiveX是在我们的Web

页中使用“历史遗留”代码的最佳途径。

6. 安全

自动下载和通过因特网运行程序听起来就像是一个病毒制造者的梦想。在客户端的编程中,

ActiveX带来了最让人头痛的安全问题。点击一个Web站点的时候,可能会随同HTML网页传回

任何数量的东西:GIF文件、脚本代码、编译好的Java代码以及ActiveX组件。有些是无害的;

GIF文件不会对我们造成任何危害,而脚本编制语言通常在自己可做的事情上有着很大的限制。Java

也设计成在一个安全“沙箱”里在它的程序片中运行,这样可防止操作位于沙箱以外的磁盘或者内

存区域。

ActiveX是所有这些里面最让人担心的。用ActiveX编写程序就像编制Windows应用程序——

可以做自己想做的任何事情。下载回一个ActiveX组件后,它完全可能对我们磁盘上的文件造成破

坏。当然,对那些下载回来并不限于在Web浏览器内部运行的程序,它们同样也可能破坏我们的

系统。从BBS下载回来的病毒一直是个大问题,但因特网的速度使得这个问题变得更加复杂。

目前解决的办法是“数字签名”,代码会得到权威机构的验证,显示出它的作者是谁。这一机

制的基础是认为病毒之所以会传播,是由于它的编制者匿名的缘故。所以假如去掉了匿名的因素,

所有设计者都不得不为它们的行为负责。这似乎是一个很好的主意,因为它使程序显得更加正规。

但我对它能消除恶意因素持怀疑态度,因为假如一个程序便含有Bug,那么同样会造成问题。

Java通过“沙箱”来防止这些问题的发生。Java解释器内嵌于我们本地的Web浏览器中,在

程序片装载时会检查所有有嫌疑的指令。特别地,程序片根本没有权力将文件写进磁盘,或者删除

文件(这是病毒最喜欢做的事情之一)。我们通常认为程序片是安全的。而且由于安全对于营建一套

可靠的客户机/服务器系统至关重要,所以会给病毒留下漏洞的所有错误都能很快得到修复(浏览

器软件实际需要强行遵守这些安全规则;而有些浏览器则允许我们选择不同的安全级别,防止对系

统不同程度的访问)。

大家或许会怀疑这种限制是否会妨碍我们将文件写到本地磁盘。比如,我们有时需要构建一个

本地数据库,或将数据保存下来,以便日后离线使用。最早的版本似乎每个人都能在线做任何敏感

的事情,但这很快就变得非常不现实(尽管低价“互联网工具”有一天可能会满足大多数用户的需

要)。解决的方案是“签了名的程序片”,它用公共密钥加密算法验证程序片确实来自它所声称的地

方。当然在通过验证后,签了名的一个程序片仍然可以开始清除你的磁盘。但从理论上说,既然现

在能够找到创建人“算帐”,他们一般不会干这种蠢事。Java 1.1为数字签名提供了一个框架,在

必要时,可让一个程序片“走”到沙箱的外面来。

数字签名遗漏了一个重要的问题,那就是人们在因特网上移动的速度。如下载回一个错误百出

的程序,而它很不幸地真的干了某些蠢事,需要多久的时间才能发觉这一点呢?这也许是几天,也

可能几周之后。发现了之后,又如何追踪当初肇事的程序呢(以及它当时的责任有多大)?

7. 因特网和内联网

Web是解决客户机/服务器问题的一种常用方案,所以最好能用相同的技术解决此类问题的一

些“子集”,特别是公司内部的传统客户机/服务器问题。对于传统的客户机/服务器模式,我们

面临的问题是拥有多种不同类型的客户计算机,而且很难安装新的客户软件。但通过Web浏览器

和客户端编程,这两类问题都可得到很好的解决。若一个信息网络局限于一家特定的公司,那么在

将Web技术应用于它之后,即可称其为“内联网”(Intranet),以示与国际性的“因特网”

(Internet)有别。内联网提供了比因特网更高的安全级别,因为可以物理性地控制对公司内部服

务器的使用。说到培训,一般只要人们理解了浏览器的常规概念,就可以非常轻松地掌握网页和程

序片之间的差异,所以学习新型系统的开销会大幅度减少。

安全问题将我们引入客户端编程领域一个似乎是自动形成的分支。若程序是在因特网上运行,

由于无从知晓它会在什么平台上运行,所以编程时要特别留意,防范可能出现的编程错误。需作一

些跨平台处理,以及适当的安全防范,比如采用某种脚本语言或者Java。

但假如在内联网中运行,面临的一些制约因素就会发生变化。全部机器均为Intel/Windows

平台是件很平常的事情。在内联网中,需要对自己代码的质量负责。而且一旦发现错误,就可以马

上改正。除此以外,可能已经有了一些“历史遗留”的代码,并用较传统的客户机/服务器方式使

用那些代码。但在进行升级时,每次都要物理性地安装一道客户程序。浪费在升级安装上的时间是

转移到浏览器的一项重要原因。使用了浏览器后,升级就变得易如反掌,而且整个过程是透明和自

动进行的。如果真的是牵涉到这样的一个内联网中,最明智的方法是采用ActiveX,而非试图采用

一种新的语言来改写程序代码。

面临客户端编程问题令人困惑的一系列解决方案时,最好的方案是先做一次投资/回报分析。

请总结出问题的全部制约因素,以及什么才是最快的方案。由于客户端程序设计仍然要编程,所以

无论如何都该针对自己的特定情况采取最好的开发途径。这是准备面对程序开发中一些不可避免的

问题时,我们可以做出的最佳姿态。

8服务器端编程

我们的整个讨论都忽略了服务器端编程的问题。如果向服务器发出一个请求,会发生什么事情?

大多数时候的请求都是很简单的一个“把这个文件发给我”。浏览器随后会按适当的形式解释这个

文件:作为HTML页、一幅图、一个Java程序片、一个脚本程序等等。向服务器发出的较复杂的

请求通常涉及到对一个数据库进行操作(事务处理)。其中最常见的就是发出一个数据库检索命令,

得到结果后,服务器会把它格式化成HTML页,并作为结果传回来(当然,假如客户通过Java或者

某种脚本语言具有了更高的智能,那么原始数据就能在客户端发送和格式化;这样做速度可以更快,

也能减轻服务器的负担)。另外,有时需要在数据库中注册自己的名字(比如加入一个组时),或者

向服务器发出一份订单,这就涉及到对那个数据库的修改。这类服务器请求必须通过服务器端的一

些代码进行,我们称其为“服务器端的编程”。在传统意义上,服务器端编程是用Perl和CGI脚本

进行的,但更复杂的系统已经出现。其中包括基于Java的Web服务器,它允许我们用Java进行

所有服务器端编程,写出的程序就叫做“小服务程序”(Servlet)。

9.一个独立的领域:应用程序

与Java有关的大多数争论都是与程序片有关的。Java实际是一种常规用途的程序设计语言,

可解决任何类型的问题,至少理论上如此。而且正如前面指出的,可以用更有效的方式来解决大多

数客户机/服务器问题。如果将视线从程序片身上转开(同时放宽一些限制,比如禁止写盘等),就

进入了常规用途的应用程序的广阔领域。这种应用程序可独立运行,毋需浏览器,就象普通的执行

程序那样。在这儿,Java的特色并不仅仅反应在它的移植能力,也反映在编程本身上。就像贯穿全

书都会讲到的那样,Java提供了许多有用的特性,使我们能在较短的时间里创建出比用从前的程序

设计语言更健壮的程序。

但要注意任何东西都不是十全十美的,我们为此也要付出一些代价。其中最明显的是执行速度

放慢了(尽管可对此进行多方面的调整)。和任何语言一样,Java本身也存在一些限制,使得它不

十分适合解决某些特殊的编程问题。但不管怎样,Java都是一种正在快速发展的语言。随着每个新

版本的发布,它变得越来越可爱,能充分解决的问题也变得越来越多。

2

14

本文标签: 问题语言程序服务器浏览器