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

📄 tij0168.html

📁 学习java的经典书籍
💻 HTML
📖 第 1 页 / 共 5 页
字号:
<html><body>

<table width="100%"><tr>
<td>
<a href="http://www.bruceeckel.com/javabook.html">Bruce Eckel's Thinking in Java</a>
</td>
<td align="right">
<a href="tij_c.html">Contents</a> | <a href="tij0167.html">Prev</a> | <a href="tij0169.html">Next</a>
</td>
</tr></table>
<hr>

<H2 ALIGN=LEFT>
Connecting
Java to CGI
<P><A NAME="Index2749"></A><A NAME="Index2750"></A></H2>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">A
Java program can send a CGI request to a server just like an HTML page can. As
with HTML pages, this request can be either a <A NAME="Index2751"></A><A NAME="Index2752"></A>GET
or a <A NAME="Index2753"></A><A NAME="Index2754"></A>POST.
In addition, the Java program can intercept the output of the CGI program, so
you don&#8217;t have to rely on the program to format a new page and force the
user to back up from one page to another if something goes wrong. In fact, the
appearance of the program can be the same as the previous version.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">It
also turns out that the code is simpler, and that CGI isn&#8217;t difficult to
write after all. (An innocent statement that&#8217;s true of many things &#8211; 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>after</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
you understand them.) So in this section you&#8217;ll get a <A NAME="Index2755"></A><A NAME="Index2756"></A>crash
course in CGI programming. To solve the general problem, some CGI tools will be
created in C++ that will allow you to easily write a CGI program to solve any
problem. The benefit to this approach is portability &#8211; the example you
are about to see will work on any system that supports CGI, and there&#8217;s
no problem with firewalls.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">This
example also works out the basics of creating any connection with applets and
CGI programs, so you can easily adapt it to your own projects.
</FONT><a name="_Toc408018777"></a><P></DIV>
<A NAME="Heading528"></A><H3 ALIGN=LEFT>
Encoding
data for CGI
</H3>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">In
this version, the name 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>and</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
the email address will be collected and stored in the file in the form:
</FONT><P></DIV><DIV ALIGN=LEFT><TT><FONT FACE="Courier New" SIZE=3 COLOR="Black">First
Last &lt;email@domain.com&gt;;
</FONT></TT><P></DIV><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">This
is a convenient form for many mailers. Since two fields are being collected,
there are no shortcuts because CGI has a particular format for encoding the
data in fields. You can see this for yourself if you make an ordinary <A NAME="Index2757"></A><A NAME="Index2758"></A>HTML
page and add the lines:
</FONT><P></DIV>

<font color="#990000"><PRE>&lt;Form method="GET" ACTION="/cgi-bin/Listmgr2.exe"&gt;
&lt;P&gt;Name: &lt;INPUT TYPE = "text" NAME = "name" 
VALUE = "" size = "40"&gt;&lt;/p&gt;
&lt;P&gt;Email Address: &lt;INPUT TYPE = "text" 
NAME = "email" VALUE = "" size = "40"&gt;&lt;/p&gt;
&lt;p&gt;&lt;input type = "submit" name = "submit" &gt; &lt;/p&gt;
&lt;/Form&gt;</PRE></font><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">This
creates two data entry fields called 
</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">,
along with a 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>submit</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
button that collects the data and sends it to a CGI program. 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Listmgr2.exe</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
is the name of the executable program that resides in the directory
that&#8217;s typically called &#8220;cgi-bin&#8221; on your Web server.
</FONT><A NAME="fnB65" HREF="#fn65">[65]</A><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
(If the named program is not in the cgi-bin directory, you won&#8217;t see any
results.) If you fill out this form and press the &#8220;submit&#8221; button,
you will see in the URL address window of the browser something like:
</FONT><P></DIV><DIV ALIGN=LEFT><TT><FONT FACE="Courier New" SIZE=3 COLOR="Black">http://www.myhome.com/cgi-bin/Listmgr2.exe?</FONT></TT><P><TT><FONT FACE="Courier New" SIZE=3 COLOR="Black">name=First+Last&amp;email=email@domain.com&amp;submit=Submit</FONT></TT><P></DIV><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">(Without
the line break, of course). Here you see a little bit of the way that data is
encoded to send to CGI. For one thing, spaces are not allowed (since spaces
typically separate command-line arguments). Spaces are replaced by &#8216;
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>+</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">&#8217;
signs. In addition, each field contains the field name (which is determined by
the HTML page) followed by an &#8216;
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>=</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">&#8217;
and the field data, and terminated by a &#8216;
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>&amp;</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">&#8217;.</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">At
this point, you might wonder about the &#8216;
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>+</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">&#8217;,
&#8216;
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>=,</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">&#8217;
and &#8216;
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>&amp;</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">&#8217;.
What if those are used in the field, as in &#8220;John &amp; Marsha
Smith&#8221;? This is encoded to: 
</FONT><P></DIV><DIV ALIGN=LEFT><TT><FONT FACE="Courier New" SIZE=3 COLOR="Black">John+%26+Marsha+Smith</FONT></TT><P></DIV><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">That
is, the special character is turned into a 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>&#8216;%</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">&#8217;
followed by its ASCII value in hex.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Fortunately,
Java has a tool to perform this encoding for you. It&#8217;s a static method of
the class <A NAME="Index2759"></A><A NAME="Index2760"></A><A NAME="Index2761"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>URLEncoder</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
called 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>encode(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
You can experiment with this method using the following program:
</FONT><P></DIV>

<font color="#990000"><PRE><font color="#009900">//: EncodeDemo.java</font>
<font color="#009900">// Demonstration of URLEncoder.encode()</font>
<font color="#0000ff">import</font> java.net.*;

<font color="#0000ff">public</font> <font color="#0000ff">class</font> EncodeDemo {
  <font color="#0000ff">public</font> <font color="#0000ff">static</font> <font color="#0000ff">void</font> main(String[] args) {
    String s = "";
    <font color="#0000ff">for</font>(<font color="#0000ff">int</font> i = 0; i &lt; args.length; i++)
      s += args[i] + " ";
    s = URLEncoder.encode(s.trim());
    System.out.println(s);
  }
} <font color="#009900">///:~ </PRE></font></font><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">This
takes the command-line arguments and combines them into a string of words
separated by spaces (the final space is removed using 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>String.trim(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">).
These are then encoded and printed.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">To
<A NAME="Index2762"></A><A NAME="Index2763"></A>invoke
a CGI program, all the applet needs to do is collect the data from its fields
(or wherever it needs to collect the data from), URL-encode each piece of data,
and then assemble it into a single string, placing the name of each field
followed by an &#8216;
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>=</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">&#8217;,
followed by the data, followed by an &#8216;
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>&amp;</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">&#8217;.
To form the entire CGI command, this string is placed after the URL of the CGI
program and a &#8216;
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>?</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">&#8217;.
That&#8217;s all it takes to invoke any CGI program, and as you&#8217;ll see
you can easily do it within an applet.
</FONT><a name="_Toc408018778"></a><P></DIV>
<A NAME="Heading529"></A><H3 ALIGN=LEFT>
The
applet
</H3>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
applet is actually considerably simpler than 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>NameSender.java</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
partly because it&#8217;s so easy to send a <A NAME="Index2764"></A><A NAME="Index2765"></A>GET
request and also because no thread is required to wait for the reply. There are
now two fields instead of one, but you&#8217;ll notice that much of the applet
looks familiar, from 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>NameSender.java</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.</FONT><P></DIV>

<font color="#990000"><PRE><font color="#009900">//: NameSender2.java</font>
<font color="#009900">// An applet that sends an email address</font>
<font color="#009900">// via a CGI GET, using Java 1.02.</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> NameSender2 <font color="#0000ff">extends</font> Applet {
  <font color="#0000ff">final</font> String CGIProgram = "Listmgr2.exe";
  Button send = <font color="#0000ff">new</font> Button(
    "Add email address to mailing list");
  TextField name = <font color="#0000ff">new</font> TextField(
    "type your name here", 40),
    email = <font color="#0000ff">new</font> TextField(
    "type your email address here", 40);
  String str = <font color="#0000ff">new</font> String();
  Label l = <font color="#0000ff">new</font> Label(), l2 = <font color="#0000ff">new</font> Label();
  <font color="#0000ff">int</font> vcount = 0;
  <font color="#0000ff">public</font> <font color="#0000ff">void</font> init() {
    setLayout(<font color="#0000ff">new</font> BorderLayout());
    Panel p = <font color="#0000ff">new</font> Panel();
    p.setLayout(<font color="#0000ff">new</font> GridLayout(3, 1));
    p.add(name);
    p.add(email);
    p.add(send);
    add("North", p);
    Panel labels = <font color="#0000ff">new</font> Panel();
    labels.setLayout(<font color="#0000ff">new</font> GridLayout(2, 1));
    labels.add(l);
    labels.add(l2);
    add("Center", labels);
    l.setText("Ready to send email address");
  }
  <font color="#0000ff">public</font> <font color="#0000ff">boolean</font> action (Event evt, Object arg) {
    <font color="#0000ff">if</font>(evt.target.equals(send)) {
      l2.setText("");
      <font color="#009900">// Check for errors in data:</font>
      <font color="#0000ff">if</font>(name.getText().trim()
         .indexOf(' ') == -1) {
        l.setText(
          "Please give first and last name");
        l2.setText("");
        <font color="#0000ff">return</font> <font color="#0000ff">true</font>;
      }
      str = email.getText().trim();
      <font color="#0000ff">if</font>(str.indexOf(' ') != -1) {
        l.setText(
          "Spaces not allowed in email name");
        l2.setText("");

⌨️ 快捷键说明

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