📄 tij0168.html
字号:
<font color="#0000ff">return</font> <font color="#0000ff">true</font>;
}
<font color="#0000ff">if</font>(str.indexOf(',') != -1) {
l.setText(
"Commas not allowed in email name");
<font color="#0000ff">return</font> <font color="#0000ff">true</font>;
}
<font color="#0000ff">if</font>(str.indexOf('@') == -1) {
l.setText("Email name must include '@'");
l2.setText("");
<font color="#0000ff">return</font> <font color="#0000ff">true</font>;
}
<font color="#0000ff">if</font>(str.indexOf('@') == 0) {
l.setText(
"Name must preceed '@' in email name");
l2.setText("");
<font color="#0000ff">return</font> <font color="#0000ff">true</font>;
}
String end =
str.substring(str.indexOf('@'));
<font color="#0000ff">if</font>(end.indexOf('.') == -1) {
l.setText("Portion after '@' must " +
"have an extension, such as '.com'");
l2.setText("");
<font color="#0000ff">return</font> <font color="#0000ff">true</font>;
}
<font color="#009900">// Build and encode the email data:</font>
String emailData =
"name=" + URLEncoder.encode(
name.getText().trim()) +
"&email=" + URLEncoder.encode(
email.getText().trim().toLowerCase()) +
"&submit=Submit";
<font color="#009900">// Send the name using CGI's GET process:</font>
<font color="#0000ff">try</font> {
l.setText("Sending...");
URL u = <font color="#0000ff">new</font> URL(
getDocumentBase(), "cgi-bin/" +
CGIProgram + "?" + emailData);
l.setText("Sent: " + email.getText());
send.setLabel("Re-send");
l2.setText(
"Waiting <font color="#0000ff">for</font> reply " + ++vcount);
DataInputStream server =
<font color="#0000ff">new</font> DataInputStream(u.openStream());
String line;
<font color="#0000ff">while</font>((line = server.readLine()) != <font color="#0000ff">null</font>)
l2.setText(line);
} <font color="#0000ff">catch</font>(MalformedURLException e) {
l.setText("Bad URl");
} <font color="#0000ff">catch</font>(IOException e) {
l.setText("IO Exception");
}
}
<font color="#0000ff">else</font> <font color="#0000ff">return</font> <font color="#0000ff">super</font>.action(evt, arg);
<font color="#0000ff">return</font> <font color="#0000ff">true</font>;
}
} <font color="#009900">///:~ </PRE></font></font><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
name of the CGI program (which you’ll see later) is
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Listmgr2.exe</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
Many Web servers are Unix machines (mine runs Linux) that don’t
traditionally use the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>.exe</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
extension for their executable programs, but you can call the program anything
you want under Unix. By using the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>.exe
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">extension
the program can be tested without change under both Unix and Win32.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">As
before, the applet sets up its user interface (with two fields this time
instead of one). The only significant difference occurs inside the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>action( )
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">method,
which handles the button press. After the name has been checked, you see the
lines:
</FONT><P></DIV>
<font color="#990000"><PRE> String emailData =
"name=" + URLEncoder.encode(
name.getText().trim()) +
"&email=" + URLEncoder.encode(
email.getText().trim().toLowerCase()) +
"&submit=Submit";
<font color="#009900">// Send the name using CGI's GET process:</font>
<font color="#0000ff">try</font> {
l.setText("Sending...");
URL u = <font color="#0000ff">new</font> URL(
getDocumentBase(), "cgi-bin/" +
CGIProgram + "?" + emailData);
l.setText("Sent: " + email.getText());
send.setLabel("Re-send");
l2.setText(
"Waiting <font color="#0000ff">for</font> reply " + ++vcount);
DataInputStream server =
<font color="#0000ff">new</font> DataInputStream(u.openStream());
String line;
<font color="#0000ff">while</font>((line = server.readLine()) != <font color="#0000ff">null</font>)
l2.setText(line); </TT><P><TT><FONT FACE="Courier New" SIZE=3 COLOR="Black"> <font color="#009900">// ... </PRE></font></font><DIV ALIGN=LEFT><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>name</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
and
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>email</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
data are extracted from their respective text boxes, and the spaces are trimmed
off both ends using <A NAME="Index2766"></A><A NAME="Index2767"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>trim( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
The
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>email</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
name is forced to lower case so all email addresses in the list can be
accurately compared (to prevent accidental duplicates based on capitalization).
The data from each field is URL-encoded, and then the GET string is assembled
in the same way that an HTML page would do it. (This way you can use a Java
applet in concert with any existing CGI program designed to work with regular
HTML GET requests.)
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">At
this point, some Java magic happens: if you want to connect to any URL, just
create a <A NAME="Index2768"></A><A NAME="Index2769"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>URL</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
object and hand the address to the constructor. The constructor makes the
connection with the server (and, with Web servers, all the action happens in
making the connection, via the string used as the URL). In this case, the URL
points to the cgi-bin directory of the current Web site (the base address of
the current Web site is produced with <A NAME="Index2770"></A><A NAME="Index2771"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>getDocumentBase( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">).
When the Web server sees “cgi-bin” in a URL, it expects that to be
followed by the name of the program inside the cgi-bin directory that you want
it to run. Following the program name is a question mark and the argument
string that the CGI program will look for in the QUERY_STRING environment
variable, as you’ll see.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Usually
when you make any sort of request, you get back (you’re forced to accept
in return) an HTML page. With Java
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>URL</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
objects, however, you can intercept anything that comes back from the <A NAME="Index2772"></A><A NAME="Index2773"></A>CGI
program by getting an
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>InputStream</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
from the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>URL</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
object. This is performed with the <A NAME="Index2774"></A><A NAME="Index2775"></A><A NAME="Index2776"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>URL</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>openStream( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
method, which is in turn wrapped in a
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>DataInputStream</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
Then you can read lines, and when
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>readLine( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
returns
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>null</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
the CGI program has finished its output.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
CGI program you’re about to see returns only one line, a string
indicating success or failure (and the details of the failure). This line is
captured and placed into the second
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Label</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
field so the user can see the results.
</FONT><P></DIV>
<A NAME="Heading530"></A><H4 ALIGN=LEFT>
Displaying
a Web page from within an applet
<P><A NAME="Index2777"></A><A NAME="Index2778"></A><A NAME="Index2779"></A></H4>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">It’s
also possible for the applet to display the result of the CGI program as a Web
page, just as if it were running in normal HTML mode. You can do this with the
following line:
</FONT><P></DIV><DIV ALIGN=LEFT><TT><FONT FACE="Courier New" SIZE=3 COLOR="Black">getAppletContext().showDocument(u);</FONT></TT><P></DIV><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><A NAME="Index2780"></A><A NAME="Index2781"></A><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
in which
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>u</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
is the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>URL</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
object. Here’s a simple example that redirects you to another Web page.
The page happens to be the output of a CGI program, but you can as easily go to
an ordinary HTML page, so you could build on this applet to produce a
password-protected gateway to a particular portion of your Web site:
</FONT><P></DIV>
<font color="#990000"><PRE><font color="#009900">//: ShowHTML.java</font>
<font color="#0000ff">import</font> java.awt.*;
<font color="#0000ff">import</font> java.applet.*;
<font color="#0000ff">import</font> java.net.*;
<font color="#0000ff">import</font> java.io.*;
<font color="#0000ff">public</font> <font color="#0000ff">class</font> ShowHTML <font color="#0000ff">extends</font> Applet {
<font color="#0000ff">static</font> <font color="#0000ff">final</font> String CGIProgram = "MyCGIProgram";
Button send = <font color="#0000ff">new</font> Button("Go");
Label l = <font color="#0000ff">new</font> Label();
<font color="#0000ff">public</font> <font color="#0000ff">void</font> init() {
add(send);
add(l);
}
<font color="#0000ff">public</font> <font color="#0000ff">boolean</font> action (Event evt, Object arg) {
<font color="#0000ff">if</font>(evt.target.equals(send)) {
<font color="#0000ff">try</font> {
<font color="#009900">// This could be an HTML page instead of</font>
<font color="#009900">// a CGI program. Notice that this CGI </font>
<font color="#009900">// program doesn't use arguments, but </font>
<font color="#009900">// you can add them in the usual way.</font>
URL u = <font color="#0000ff">new</font> URL(
getDocumentBase(),
"cgi-bin/" + CGIProgram);
<font color="#009900">// Display the output of the URL using</font>
<font color="#009900">// the Web browser, as an ordinary page:</font>
getAppletContext().showDocument(u);
} <font color="#0000ff">catch</font>(Exception e) {
l.setText(e.toString());
}
}
<font color="#0000ff">else</font> <font color="#0000ff">return</font> <font color="#0000ff">super</font>.action(evt, arg);
<font color="#0000ff">return</font> <font color="#0000ff">true</font>;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -