⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch24.htm

📁 CGI programming is the hottest stuff to look out for in this book
💻 HTM
📖 第 1 页 / 共 2 页
字号:
<HTML>

<HEAD>
   <TITLE>Chapter 24 -- Java and JavaScript as Alternatives to CGI </TITLE>
   <META>
</HEAD>
<BODY TEXT="#000000" BGCOLOR="#FFFFFF" LINK="#0000EE" VLINK="#551A8B" ALINK="#CE2910">
<H1><FONT COLOR=#FF0000>Chapter 24</FONT></H1>
<H1><B><FONT SIZE=5 COLOR=#FF0000>Java and JavaScript as Alternatives to CGI</FONT></B>
</H1>
<P>
<HR WIDTH="100%"></P>
<P>
<H3 ALIGN=CENTER><FONT COLOR="#000000"><FONT SIZE=+2>CONTENTS<A NAME="CONTENTS"></A>
</FONT></FONT></H3>


<UL>
<LI><A HREF="#JavaBringingMoreDynamicstotheWeb" >Java: Bringing More Dynamics to the Web</A>
<LI><A HREF="#WhatIsJavaScript" >What Is JavaScript?</A>
<LI><A HREF="#DifferencesbetweenJavaandJavaScript" >Differences between Java and JavaScript</A>
<LI><A HREF="#ComparisonofCGIandJavaJavaScript" >Comparison of CGI and Java/JavaScript</A>
<LI><A HREF="#CommunicationbetweenJavaandCGI" >Communication between Java and CGI</A>
<LI><A HREF="#JavatoCGI" >Java to CGI</A>
<LI><A HREF="#CGItoJava" >CGI to Java</A>
<LI><A HREF="#JavaandJavaScript" >Java and JavaScript</A>
<LI><A HREF="#JavaScript" >JavaScript</A>
<LI><A HREF="#EachTechniqueHasItsPlace" >Each Technique Has Its Place</A>
<LI><A HREF="#FutureImprovements" >Future Improvements</A>
<LI><A HREF="#WheretoGetMoreInformation" >Where to Get More Information</A>
<LI><A HREF="#Summary" >Summary</A>
</UL>
<HR>
<P>
Java and JavaScript are two languages you can use as an alternative
to using CGI applications. The CGI specifications are respected
by programs that run on the server side-the Web server. But Java
and JavaScript are technologies that allow execution of special
programs or actions on the client side. So, Java and JavaScript
are not related in any way with the CGI specifications, but its
study as an alternative to CGI programming is interesting and
can be useful in a lot of applications. This chapter presents
both technologies and compares them to CGI. We cover the usefulness
of each programming alternative in the development of Web applications.
<P>
References to resources on the Internet that cover Java, JavaScript,
and the CGI specification are also given so that you can explore
these technologies further and make the best choice when you plan
a Web application.
<H2><A NAME="JavaBringingMoreDynamicstotheWeb"><FONT SIZE=5 COLOR=#FF0000>Java:
Bringing More Dynamics to the Web</FONT></A></H2>
<P>
Java is an object-oriented programming language developed by Sun
Microsystems and is somewhat similar to C++. Its development started
back in 1990, but the official announcement of the Java language
and support material was made in 1995. Java brings more interactivity
to World Wide Web documents because it allows one to do things
that were otherwise impossible or hard to do with current Web
technology. In fact, because Java is a programming language, it
enables you to create both standalone programs and programs that
can be embedded in Web pages using special tags. By giving the
possibility of inserting a program inside a Web page, Java opens
a lot of new horizons to information presentation on the Web.
<P>
With Java, Web pages can become real applications instead of plain
documents containing static information. Java programs (commonly
called Java <I>applets</I>) embedded in HTML pages with the <TT><FONT FACE="Courier">&lt;APPLET&gt;</FONT></TT>
tag are always executed on the client side-the browser. It is
not the program itself that you embed in the documents but the
program name along with its parameters.
<P>
You do not have to install each program you execute. In fact,
it's up to the browser to interpret Java applets and execute the
actions. The browser requests a server for a document, requests
the associated applets, and executes them. Java applets are not
restricted by network bandwidth (once they are transferred to
the local computer, they are executed as fast as the computer
allows it) or server overload, as CGI applications may be.
<P>
The difference between Java and browser plug-in applications is
that you do not have to install a plug-in for each different type
of information you want to display (if you consider using a new
type of information). With Java, you request a program each time-not
a plain data file of various formats-that may do virtually anything
inside a Web page.
<P>
Imagine that a new image format has appeared and you do not have
a browser that supports it. If you connect to a normal page with
the image embedded, you will not be able to view it unless you
download a browser plug-in or helper application for your browser.
But if you connect to a page containing a special Java applet,
it is the Java applet itself that runs on your computer and is
responsible for the image retrieval and display. With a programming
language like this embedded in Web pages, you can create lots
of different applications: animations, spreadsheets, automatically
updated graphics embedded in a page, games, and so on.
<P>
A Java applet can be run on different computer platforms, so it
cannot be compiled for a particular platform. A solution would
be the interpretation of the Java program, but that could result
in very slow applications. Java is a hybrid solution between these
two. In fact, a Java applet is compiled to a special byte code-an
intermediary representation of the program-and it is the byte
code that is executed by the Java &quot;interpreter&quot; (it
does not interpret Java sources; it interprets Java byte codes).
This way, every machine with a Java interpreter can run a Java
program, even if it is developed in a different platform and is
provided by a different Web server.
<P>
A Java program in source version-something.java-must be compiled
before execution by the client. The name of the compiled version
becomes something.class. One typical programming example is the
&quot;Hello World!&quot; string. Let's look at the source:
<BLOCKQUOTE>
<TT><FONT FACE="Courier">Class HelloWorld [<BR>
&nbsp;&nbsp;&nbsp;&nbsp;public static void main (String args[]);
<BR>
&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(&quot;Hello World!&quot;);
<BR>
}</FONT></TT>
</BLOCKQUOTE>
<P>
This program can be compiled by using the Java compiler (<TT><FONT FACE="Courier">javac</FONT></TT>),
distributed by Sun with the Java Development Kit.
<P>
Java is currently supported by the Netscape and HotJava browsers,
but version 3.0 of Microsoft Internet Explorer (the final version,
after the beta test phase, will be out by the time you read this)
will support it, as well. The HotJava browser is a Java-compatible
browser developed by Sun and written in Java! Remember the chicken
and the egg story?
<P>
You can find out more about Java, the Java Development Kit, and
some Java applets at the following URLs:
<UL>
<LI><TT><FONT FACE="Courier"><A HREF="http://www.javasoft.com/">http://www.javasoft.com/</A></FONT></TT>
<LI><TT><FONT FACE="Courier"><A HREF="http://www.applets.com/">http://www.applets.com/</A></FONT></TT>
<LI><TT><FONT FACE="Courier"><A HREF="http://www.gamelan.com/">http://www.gamelan.com/</A></FONT></TT>
</UL>
<H2><A NAME="WhatIsJavaScript"><FONT SIZE=5 COLOR=#FF0000>What
Is JavaScript?</FONT></A></H2>
<P>
JavaScript is not another version of the Java programming language,
nor is it any new project of the Java team. Following the release
of Java, Netscape continued to push the development of the World
Wide Web further and released JavaScript as another means of adding
client-side programs to Web documents.
<P>
Netscape initially called JavaScript Mocha and then LiveScript.
Its aim was to create a programming language that could be embedded
in Web pages. Unlike Java, an HTML page does not have an applet
tag referencing the actual Java program, but the program itself
is embedded in the HTML source, surrounded by the <TT><FONT FACE="Courier">&lt;SCRIPT&gt;</FONT></TT>
tag! Programs in Web pages are called <I>scripts</I> instead of
applets or applications.
<P>
JavaScript was intended mainly to help recognition and response
to user events such as mouse clicks or forms submissions (a JavaScript
program can check for the user's input before it is sent to the
server).
<P>
JavaScript is also executed on the client-side, therefore lessening
the load on a Web server. JavaScript programs are usually less
complex and smaller than Java applets and do not have to be compiled
before execution. They can be written directly in a Web page and
executed (interpreted) by the browser that requests it. JavaScript
is currently supported by the Netscape browser. JavaScript programs
are embedded in HTML pages through the use of the <TT><FONT FACE="Courier">&lt;SCRIPT
LANGUAGE=&quot;JavaScript&quot;&gt; ... &lt;/SCRIPT&gt;</FONT></TT>
tags. The typical &quot;Hello World!&quot;program is presented
in Listing 24.1.<BR>
<HR>
<BLOCKQUOTE>
<B>Listing 24.1. The Hello World! program.<BR>
</B>
</BLOCKQUOTE>
<BLOCKQUOTE>
<TT><FONT FACE="Courier">&lt;HTML&gt;<BR>
&lt;HEAD&gt;<BR>
&lt;SCRIPT LANGUAGE=&quot;JavaScript&quot;&gt;<BR>
&lt;!--<BR>
document.write(&quot;Hello world!&quot;);<BR>
//--&gt;<BR>
&lt;/SCRIPT&gt;<BR>
&lt;/HEAD&gt;<BR>
&lt;BODY&gt;<BR>
&lt;/BODY&gt;<BR>
&lt;/HTML&gt;</FONT></TT>
</BLOCKQUOTE>
<HR>
<H2><FONT SIZE=5 COLOR=#FF0000><A NAME="DifferencesbetweenJavaandJavaScript">Differences between Java and JavaScript</A></FONT>
</H2>
<P>
Java and JavaScript have some similarities but more differences
than their names may reveal. JavaScript is a scripting language
and HTML-page oriented (the script is embedded in the HTML source),
while Java is a complete programming language that can be used
in and outside the Web world (standalone programs may be developed
using Java, for example).
<P>
On one hand, Java applets are only referenced inside the HTML
source of a page and executed inside the browser's window. JavaScript
scripts are actually embedded in the HTML source and are also
executed by the browser on the document's window.
<P>
Java programs consist of classes and respective methods, and their
objects are declared and safely typed. JavaScript is a smaller
language with an easier syntax that has some data types already
built-in.
<P>
Another difference comes from the execution strategy. JavaScript
programs are interpreted while Java applets must be precompiled
before execution (a pseudo-interpretation, in fact). Object references
in JavaScript are checked at runtime while in Java they exist
at compile time.
<P>
Both languages pretend to be secure, and Web programs developed
with them cannot (or should not), in particular, write to the
hard disk. Java also has security features concerning network
functioning. Java seems to comply with security issues better
than JavaScript. You can check out <TT><FONT FACE="Courier"><A HREF="http://www.osf.org/~loverso/javascript/">http://www.osf.org/~loverso/javascript/</A></FONT></TT>
for a list of known JavaScript bugs.
<H2><A NAME="ComparisonofCGIandJavaJavaScript"><FONT SIZE=5 COLOR=#FF0000>Comparison
of CGI and Java/JavaScript</FONT></A></H2>
<P>
CGI applications are closely related to a Web server, both-application
and server-respecting the CGI specifications. Also, CGI applications
are executed on the server side, while Java or JavaScript programs
are executed on the client side.
<P>
In general, applications that require a lot of processing on a
server, such as accessing or controlling a database, gateways,
or other Internet services or protocols, are better developed
with CGI applications. But if you plan to add some dynamics on
Web pages, execute animations, graphics, movements, or other features,
you will probably be better served by Java and possibly JavaScript.
Being executed on the server side, CGI applications put some extra
load on a Web server instead of on the client side, as Java and
JavaScript do.
<P>
Due to its architecture and available system tools (that can be
used by a CGI application), a CGI application can easily use the
hard disk of a server or initiate network connections on the Internet.
Java and JavaScript programs are not able to access directly the
server's disk or initiate network connections easily enough (or
at all in the case of JavaScript). CGI applications, in fact,
are not limited to one programming language. They are limited
only by the CGI specifications, which are general enough to allow
the use of any programming language, such as C, Perl, C++, and

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -