📄 ch24.htm
字号:
so on, and consequently, the characteristics of any of these languages.
Java applets are not an interface definition but are actual programs.
<H2><A NAME="CommunicationbetweenJavaandCGI"><FONT SIZE=5 COLOR=#FF0000>Communication
between Java and CGI</FONT></A></H2>
<P>
Java and CGI applications often serve different needs and can
be complementary tools. In many situations, you may choose to
develop your application by using CGI specifications or by using
Java. But it is also possible for you to use both technologies
because how they complement one another can be of great use. Fortunately
enough, CGI applications may call Java applets, and vice versa,
so that both can communicate and offer better applications on
the Web.
<H2><A NAME="JavatoCGI"><FONT SIZE=5 COLOR=#FF0000>Java to CGI</FONT></A>
</H2>
<P>
Java applets can call CGI applications with arguments, just like
a simple HTML page. This works for CGI calls with the <TT><FONT FACE="Courier">GET</FONT></TT>
method:
<BLOCKQUOTE>
<TT><FONT FACE="Courier"> <BR>
cgiScript = "<A HREF="http://www.something.com/cgi-bin/add.htm?6+plants">http://www.something.com/cgi-bin/add.htm?6+plants</A>";
<BR>
getAppletContext().showDocument(new URL cgiScript));<BR>
</FONT></TT>
</BLOCKQUOTE>
<H2><A NAME="CGItoJava"><FONT SIZE=5 COLOR=#FF0000>CGI to Java</FONT></A>
</H2>
<P>
Imagine that you'd like users of your pages to pass arguments
to Java applets in the same way they do for CGI applications,
like this:
<BLOCKQUOTE>
<TT><FONT FACE="Courier">http://www.something.com/add.html?5+fish</FONT></TT>
</BLOCKQUOTE>
<P>
One such possibility is to create an applet call with the following
arguments:
<BLOCKQUOTE>
<TT><FONT FACE="Courier"><APPLET CODE=example.class width=400
height=300><BR>
<PARAM NAME=quantity VALUE=5><BR>
<PARAM NAME=type VALUE=1><BR>
</APPLET></FONT></TT>
</BLOCKQUOTE>
<P>
A more elegant solution is to send arguments to a CGI application
in the normal way and use this application as a wrapper to identify
the arguments and generate the proper applet call.
<H2><A NAME="JavaandJavaScript"><FONT SIZE=5 COLOR=#FF0000>Java
and JavaScript</FONT></A></H2>
<P>
Java and JavaScript are expected to communicate between each other
in future versions of the Netscape browser. A JavaScript function
could capture an event (mouse click, form submission, and so on)
and pass commands to a Java applet, or a Java applet could generate
events for JavaScript to capture, for example.
<H2><A NAME="JavaScript"><FONT SIZE=5 COLOR=#FF0000>JavaScript</FONT></A>
</H2>
<P>
An interesting possibility for JavaScript is the use of server-side
includes (SSI) to insert modules at some place in the code:
<BLOCKQUOTE>
<TT><FONT FACE="Courier"><SCRIPT><BR>
<!--"include file="lib.js"--><BR>
doSomething()<BR>
</SCRIPT></FONT></TT>
</BLOCKQUOTE>
<P>
The page is pre-parsed by the server, so each SSI tag is replaced
by the server with the associated program results or file. This
way, modules (pieces of JavaScript code) can be created and organized
in different files and included in a page only when needed. Instead
of including a file, the SSI tag could execute a program that
would output the code for the modules. These modules could, for
instance, make use of some of the environment variables passed
from the server to the SSI script.
<H2><A NAME="EachTechniqueHasItsPlace"><FONT SIZE=5 COLOR=#FF0000>Each
Technique Has Its Place</FONT></A></H2>
<P>
When developing Web applications, one has several choices on the
horizon: Java, JavaScript, and CGI applications. CGI applications
can also be developed by using one or more of the various computer
languages available, depending on the aim of the program and on
the experience one has.
<P>
Java is not the ultimate application language, and neither is
JavaScript. You should learn to live with all these possibilities,
putting more emphasis on Java and CGI applications. Java is client-side
oriented and well suited for custom user interfaces or applications
that can depend solely on the local computer resources (such as
animations, calculations, games, and so on). CGI applications
are well suited for most server-side dependent actions, such as
accessing databases, decoding form contents, or using gateways
to other protocols and services on the Internet.
<P>
Also, Java is being used to create many special effects and more
dynamic Web pages. One such example is presented in Figure 24.1.
It is a scrolling message applet, which is useful to show users
important messages or to catch their attention to some event.
The same effect is also possible with JavaScript, and its source
is presented in Listing 24.2.</p>
<A HREF="f24-1.gif"><B>Figure 24.1:</B><I>The scrolling message applet.</I></A>
<HR>
<BLOCKQUOTE>
<B>Listing 24.2. The JavaScript scrolling message source.<BR>
</B>
</BLOCKQUOTE>
<BLOCKQUOTE>
<TT><FONT FACE="Courier"><HTML><BR>
<HEAD><BR>
<TITLE>Scroll bar JavaScript page</TITLE><BR>
<SCRIPT LANGUAGE="JavaScript"><BR>
<!--<BR>
var MyString="Look at this. Isn't it simple? "
<BR>
var timer=0<BR>
function Slide() {<BR>
document.box.boxtext.value=MyString<BR>
MyString=MyString.substring(1,MyString.length)+MyString.charAt(0)
<BR>
timer=setTimeout("Slide()",200)
<BR>
}<BR>
//--><BR>
</SCRIPT><BR>
</HEAD><BR>
<BODY onLoad="Slide()"><BR>
<H1 ALIGN=center>JavaScript scroll bar page</H1><BR>
<P><BR>
<CENTER><BR>
<FORM NAME="box" onSubmit="0"><BR>
<INPUT TYPE="text" NAME="boxtext" SIZE=40
VALUE=""><BR>
</FORM><BR>
</CENTER><BR>
</BODY><BR>
</HTML></FONT></TT>
</BLOCKQUOTE>
<HR>
<P>
Another funny Java applet is the visitor counter. Instead of a
static counter that displays the number of visitors to a page,
the dynamic Java counter always shows updated results without
user intervention (such as pressing the reload button); it really
turns while you're visiting a page! Check out <TT><FONT FACE="Courier"><A HREF="http://www-net.com/java/faq/">http://www-net.com/java/faq/</A></FONT></TT>
for an example of this counter. The counter is displayed in Figure
24.2.</p>
<A HREF="f24-2.gif"><B>Figure 24.2:</B><I>An auto-updated counter.</I></A>
<P>
Is there really a need to learn Java, JavaScript, and CGI programming?
The answer is yes. If you want to do serious work in Web programming,
you should probably learn the CGI specifications and some common
CGI applications languages, such as Perl or C. Java can enhance
your pages, make them more attractive, or help you develop some
serious applications, so it would be nice to learn it, too. Many
people think it is the future of programming in general, so the
time passed learning Java will surely not be wasted time. JavaScript
is a simpler language, Netscape-proprietary, that enables one
to create some special effects on Web pages, without the need
of a more complex Java program and a compilation process.
<P>
Will Java make CGI applications obsolete and, for example, eliminate
the use of Perl? Surely not. CGI applications are the most suitable
solution to many problems. Perl text-parsing and handling, for
example, are far superior to Java's, while Java applets can handle
other functions that Perl programs cannot (because of the server-side
execution, for example).
<H2><A NAME="FutureImprovements"><FONT SIZE=5 COLOR=#FF0000>Future
Improvements</FONT></A></H2>
<P>
Most of the time, network available bandwidth is the bottleneck
for every Web application. Sometimes, even the small Java applets
suffer from slow downloading. Java is quite slow in computers
in which normal compiled programs run fast (such as a low-end
486). We expect that both network bandwidth, to allow Java network-intensive
applications, and computational power will make Java a widespread
language. Also, there are not many tools (compilers, debuggers,
viewers, and so on) available today to support Java development.
Some exist, but the future will probably bring us improved development
tools.
<H2><A NAME="WheretoGetMoreInformation"><FONT SIZE=5 COLOR=#FF0000>Where
to Get More Information</FONT></A></H2>
<P>
See the following for more information:
<P>
<B>JavaSoft Home Page</B>
<BLOCKQUOTE>
<TT><FONT FACE="Courier"><A HREF="http://www.javasoft.com/">http://www.javasoft.com/</A></FONT></TT>
</BLOCKQUOTE>
<P>
<B>Gamelan</B>
<BLOCKQUOTE>
<TT><FONT FACE="Courier"><A HREF="http://www.gamelan.com/">http://www.gamelan.com/</A></FONT></TT>
</BLOCKQUOTE>
<P>
<B>Javaworld</B>
<BLOCKQUOTE>
<TT><FONT FACE="Courier"><A HREF="http://www.javaworld.com/">http://www.javaworld.com/</A></FONT></TT>
</BLOCKQUOTE>
<P>
<B>Java FAQ Archives</B>
<BLOCKQUOTE>
<TT><FONT FACE="Courier"><A HREF="http://www-net.com/java/faq/">http://www-net.com/java/faq/</A></FONT></TT>
</BLOCKQUOTE>
<P>
<B>JavaScript Authoring Guide</B>
<BLOCKQUOTE>
<TT><FONT FACE="Courier"><A HREF="http://www.netscape.com/eng/mozilla/2.0/handbook/javascript/index.html">http://www.netscape.com/eng/mozilla/2.0/handbook/javascript/index.html</A></FONT></TT>
</BLOCKQUOTE>
<P>
<B>JavaScript Index</B>
<BLOCKQUOTE>
<TT><FONT FACE="Courier"><A HREF="http://www.c2.org/~andreww/javascript/">http://www.c2.org/~andreww/javascript/</A></FONT></TT>
</BLOCKQUOTE>
<P>
<B>JavaScript Resource Center</B>
<BLOCKQUOTE>
<TT><FONT FACE="Courier"><A HREF="http://www.intercom.net/user/mecha/java/links.html">http://www.intercom.net/user/mecha/java/links.html</A></FONT></TT>
</BLOCKQUOTE>
<P>
<B>Bugs</B>
<BLOCKQUOTE>
<TT><FONT FACE="Courier"><A HREF="http://www.osf.org/~loverso/javascript/">http://www.osf.org/~loverso/javascript/</A></FONT></TT>
</BLOCKQUOTE>
<H2><A NAME="Summary"><FONT SIZE=5 COLOR=#FF0000>Summary</FONT></A>
</H2>
<P>
Java and JavaScript are two alternatives to CGI programming. This
chapter presented Java and JavaScript functionality and covered
both its similarities and its differences. Java is a complete
programming language and can substitute for CGI applications in
many cases, while JavaScript is a nice scripting language that
can help one put interesting effects on an HTML page with a simple
embedded program.
<P>
<HR WIDTH="100%"></P>
<CENTER><P><A HREF="ch23.htm"><IMG SRC="pc.gif" BORDER=0 HEIGHT=88 WIDTH=140></A><A HREF="#CONTENTS"><IMG SRC="cc.gif" BORDER=0 HEIGHT=88 WIDTH=140></A><A HREF="index.htm"><IMG SRC="hb.gif" BORDER=0 HEIGHT=88 WIDTH=140></A><A HREF="ch25.htm"><IMG
SRC="nc.gif" BORDER=0 HEIGHT=88 WIDTH=140></A></P></CENTER>
<P>
<HR WIDTH="100%"></P>
</BODY>
</HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -