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

📄 tij0167.html

📁 学习java的经典书籍
💻 HTML
📖 第 1 页 / 共 4 页
字号:
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Label</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">s
are used to report status back to the user.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">By
now you can recognize the <A NAME="Index2725"></A><A NAME="Index2726"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>DatagramSocket</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
<A NAME="Index2727"></A><A NAME="Index2728"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>InetAddress</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
buffer, and <A NAME="Index2729"></A><A NAME="Index2730"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>DatagramPacket</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
as trappings of the network connection. Lastly, you can see the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>run(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
method that implements the thread portion so the applet can listen for the
reply sent back by the server.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>init(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
method sets up the GUI with the familiar layout tools, then creates the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>DatagramSocket</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
that will be used both for sending and receiving datagrams.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
<A NAME="Index2731"></A><A NAME="Index2732"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>action(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
method (remember, we&#8217;re confined to Java 1.0<A NAME="Index2733"></A>
now, so we can&#8217;t use any slick inner listener classes) watches only to
see if you press the &#8220;send&#8221; button. When the button is pressed, the
first action is to check the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Thread
pl
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
to see if it&#8217;s 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>null</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
If it&#8217;s not 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>null</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
there&#8217;s a live thread running. The first time the message is sent a
thread is started up to watch for the reply. Thus, if a thread is running, it
means this is not the first time the user has tried to send the message. The 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>pl
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">handle
is set to 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>null</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
and the old listener is interrupted. (This is the preferred approach, since <A NAME="Index2734"></A><A NAME="Index2735"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>stop(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
is deprecated in Java 1.2<A NAME="Index2736"></A>
as explained in the previous chapter.)
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Regardless
of whether this is the first time the button was pressed, the text in 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>l2</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
is erased.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
next group of statements checks the email name for errors. The <A NAME="Index2737"></A><A NAME="Index2738"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>String.indexOf(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
method is used to search for illegal characters, and if one is found it is
reported to the user. Note that all of this happens without any network
activity, so it&#8217;s fast and it doesn&#8217;t bog down the Internet.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Once
the name is verified, it is packaged into a datagram and sent to the host
address and port number in the same way that was described in the earlier
datagram example. The first label is changed to show you that the send has
occurred, and the button text is changed so that it reads
&#8220;re-send.&#8221; At this point, the thread is started up and the second
label informs you that the applet is waiting for a reply from the server. 
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>run(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
method for the thread uses the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>DatagramSocket</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
that lives in 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>NameSender</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
to <A NAME="Index2739"></A><A NAME="Index2740"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>receive(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
which blocks until the datagram packet comes from the server. The resulting
packet is placed into 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>NameSender</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">&#8217;s
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>DatagramPacket
dp
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
The data is retrieved from the packet and placed into the second label in 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>NameSender</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
At this point, the thread terminates and becomes dead. If the reply
doesn&#8217;t come back from the server in a reasonable amount of time, the
user might become impatient and press the button again, thus terminating the
current thread (and, after re-sending the data, starting a new one). Because a
thread is used to listen for the reply, the user still has full use of the UI.
</FONT><P></DIV>
<A NAME="Heading525"></A><H4 ALIGN=LEFT>
The
Web page
</H4>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Of
course, the applet must go inside a Web page. Here is the complete Web page;
you can see that it&#8217;s intended to be used to automatically collect names
for my mailing list:
</FONT><P></DIV>

<font color="#990000"><PRE>&lt;HTML&gt;
&lt;HEAD&gt;
&lt;META CONTENT="text/html"&gt;
&lt;TITLE&gt;
Add Yourself to Bruce Eckel's Java Mailing List
&lt;/TITLE&gt;
&lt;/HEAD&gt;
&lt;BODY LINK="#0000ff" VLINK="#800080" BGCOLOR="#ffffff"&gt;
&lt;FONT SIZE=6&gt;&lt;P&gt;
Add Yourself to Bruce Eckel's Java Mailing List
&lt;/P&gt;&lt;/FONT&gt;
The applet on <font color="#0000ff">this</font> page will automatically add your email address to the mailing list, so you will receive update information about changes to the online version of "Thinking in Java," notification when the book is in print, information about upcoming Java seminars, and notification about the &#8220;Hands-on Java Seminar&#8221; Multimedia CD. Type in your email address and press the button to automatically add yourself to <font color="#0000ff">this</font> mailing list. &lt;HR&gt;
&lt;applet code=NameSender width=400 height=100&gt;
&lt;/applet&gt;
&lt;HR&gt;
If after several tries, you <font color="#0000ff">do</font> not get verification it means that the Java application on the server is having problems. In <font color="#0000ff">this</font> <font color="#0000ff">case</font>, you can add yourself to the list by sending email to 
&lt;A HREF="mailto:Bruce@EckelObjects.com"&gt;
Bruce@EckelObjects.com&lt;/A&gt;
&lt;/BODY&gt;</PRE></font><DIV ALIGN=LEFT><TT><FONT FACE="Courier New" SIZE=2 COLOR="Black">&lt;/HTML&gt;</FONT></TT><P></DIV><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
applet tag is quite trivial, no different from the first one presented in
Chapter 13.
</FONT><a name="_Toc408018775"></a><P></DIV>
<A NAME="Heading526"></A><H3 ALIGN=LEFT>
Problems
with this approach
</H3>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">This
certainly seems like an elegant approach. There&#8217;s no CGI programming and
so there are no delays while the server starts up a CGI program. The datagram
approach seems to produce a nice quick response. In addition, when Java 1.1<A NAME="Index2741"></A>
is available everywhere, the server portion can be written entirely in Java.
(Although it&#8217;s quite interesting to see how easy it is to connect to a
non-Java program using standard input and output.)
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">There
are problems, however. One problem is rather subtle: since the Java application
is running constantly on the server and it spends most of its time blocked in
the <A NAME="Index2742"></A><A NAME="Index2743"></A><A NAME="Index2744"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Datagram.receive(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
method, there 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>might</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
be some CPU hogging going on. At least, that&#8217;s the way it appeared on the
server where I was experimenting. On the other hand, there wasn&#8217;t much
else happening on that server, and starting the program using
&#8220;nice&#8221; (a Unix program to prevent a process from hogging the CPU)
or its equivalent could solve the problem if you have a more heavily-loaded
server. In any event, it&#8217;s worth keeping your eye on an application like
this &#8211; a blocked 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>receive(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
could hog the CPU.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
second problem is a show stopper. It concerns firewalls. A <A NAME="Index2745"></A><A NAME="Index2746"></A>firewall
is a machine that sits between your network and the Internet. It monitors all
traffic coming in from the Internet and going out to the Internet, and makes
sure that traffic conforms to what it expects.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Firewalls
are conservative little beasts. They demand strict conformance to all the
rules, and if you&#8217;re not conforming they assume that you&#8217;re doing
something sinful and shut you out (not quite so bad as the Spanish Inquisition,
but close). For example, if you are on a network behind a firewall and you
start connecting to the Internet using a Web browser, the firewall expects that
all your transactions will connect to the server using the accepted http port,
which is 80. Now along comes this Java applet 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>NameSender,</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
which is trying to send a datagram to port 8080, which is way outside the range
of the &#8220;protected&#8221; ports 0-1024. The firewall naturally assumes the
worst &#8211; that someone has a virus &#8211; and it doesn&#8217;t allow the
transaction to happen.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">As
long as your customers have raw connections to the Internet (for example, using
a typical Internet service provider) there&#8217;s no problem, but you might
have some important customers dwelling behind firewalls, and they won&#8217;t
be able to use your program.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">This
is rather disheartening after learning so much Java, because it would seem that
you must give up Java on the server and learn how to write CGI scripts in C or
Perl. But as it turns out, despair is not in order.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">One
scenario is part of Sun&#8217;s grand scheme. If everything goes as planned,
Web servers will be equipped with <A NAME="Index2747"></A><A NAME="Index2748"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>servlet
servers
</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
These will take a request from the client (going through the firewall-accepted
port 80) and instead of starting up a CGI program they will start up a Java
program called a 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>servlet</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
This is a little application that&#8217;s designed to run only on the server. A
servlet server will automatically start up the servlet to handle the client
request, which means you can write all your programs in Java (further enabling
the &#8220;100 percent pure Java initiative&#8221;). It is admittedly an
appealing idea: once you&#8217;re comfortable with Java, you don&#8217;t have
to switch to a more primitive language to handle requests on the server.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Since
it&#8217;s only for handling requests on the server, the servlet API has no GUI
abilities. This fits quite well with 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>NameCollector.java</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
which doesn&#8217;t have a GUI anyway.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">At
this writing, a low-cost servlet server was available from 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>java.sun.com</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
In addition, Sun is encouraging other Web server manufacturers to add servlet
capabilities to their servers.
</FONT><a name="_Toc408018776"></a><P></DIV>

<div align="right">
<a href="tij_c.html">Contents</a> | <a href="tij0166.html">Prev</a> | <a href="tij0168.html">Next</a>
</div>
</body></html>

⌨️ 快捷键说明

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