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

📄 ch05.htm

📁 Web_Programming_with_Perl5,一个不错的Perl语言教程。
💻 HTM
📖 第 1 页 / 共 5 页
字号:
	<TR ALIGN="LEFT" rowspan="1">



		<TD ALIGN="LEFT" VALIGN="TOP"><TT>502</TT></TD>



		<TD WIDTH="104" ALIGN="LEFT" VALIGN="TOP"><TT>Bad Gateway</TT></TD>



		<TD ALIGN="LEFT">The server received an invalid response from the gateway or an upstream server.</TD>



	</TR>



	<TR ALIGN="LEFT" rowspan="1">



		<TD ALIGN="LEFT" VALIGN="TOP"><TT>503</TT></TD>



		<TD WIDTH="104" ALIGN="LEFT" VALIGN="TOP"><TT>Service Unavailable</TT></TD>



		<TD ALIGN="LEFT">The server cannot process the request due to a high load or maintenance of the server.</TD>



	</TR>



	<TR ALIGN="LEFT" rowspan="1">



		<TD ALIGN="LEFT" VALIGN="TOP"><FONT COLOR="#000000">504</FONT></TD>



		<TD WIDTH="104" ALIGN="LEFT" VALIGN="TOP"><FONT COLOR="#000000">Gateway Timeout</FONT></TD>



		<TD ALIGN="LEFT"><FONT COLOR="#000000">The response did not return within a time that the gateway



			was prepared to wait. Similar to Internal error 500 but has more diagnostic value.</FONT></TD>



	</TR>



</TABLE>



In the next section, we will look at how the <TT>header()</TT> method in CGI.pm can



be used in your script to set the <TT>Status</TT> response header. <B><TT>Content-Type



Response Header</TT></B> The most common response header is <TT>Content-Type</TT>,



which contains the MIME type of the object being returned (usually &quot;text/html&quot;),



and <TT>Content-Length</TT>, which indicates the size of the object. <B><TT>Location



Response Header</TT></B> If you did not want to return a file but instead wanted



your script to send the browser to another URI, the <TT>Location</TT> response header



can be used.</P>



<P>The headers are terminated by an empty line and followed by the &quot;body&quot;



of the message. The &quot;body&quot; refers to the text or HTML or other MIME compliant



message in the object.</P>



<P>So, bringing all this together, here is the syntax of a basic HTTP response header:</P>



<PRE><FONT COLOR="#0066FF">&quot;HTTP/1.0&quot; result-code message CrLf



Header: Value CrLf



Header: Value CrLf



CrLf



BODY



Connection closed by foreign host.



</FONT></PRE>



<P>Here is a real-world example of the complete response generated by a Netscape



Communications Server. This response was generated after a simple <TT>GET</TT> request



was sent for an HTML file. A similar response could be generated by your CGI script



using the <TT>header()</TT> method in CGI.pm.</P>



<PRE><FONT COLOR="#0066FF">HTTP/1.0 200 OK



Server: Netscape-Communications/1.12



Date: Monday, 21-Oct-96 01:42:01 GMT



Last-modified: Thursday, 10-Oct-96 17:17:20 GMT



Content-length: 1048



Content-type: text/html



&lt;HTML&gt;



&lt;HEAD&gt;



&lt;TITLE&gt;Welcome to My Web Page&lt;/TITLE&gt;



&lt;/HEAD&gt;



&lt;BODY&gt;



Here is my web page.  &lt;b&gt;Do you like it?&lt;/b&gt;



&lt;/BODY&gt;



&lt;/HTML&gt;



Connection closed by foreign host.



</FONT></PRE>



<P>Notice that after the response headers are sent, a blank line (CrLf) is sent,



which is followed by the body of the response. The body is the Web page, or the data



your script outputs. After the data has been sent (the body), the server drops the



connection.</P>



<P>When generating your own response headers for NPH scripts (highly recommended),



the contents of these response headers are easily modified using CGI.pm, or the HTTP::Response



class modules. In the next section, I will show you how CGI.pm or the HTTP::Response



class can be used to easily generate response headers.



<CENTER>



<H3><A NAME="Heading14"></A><FONT COLOR="#000077">Perl5 and the WWW Libraries in



Action</FONT></H3>



</CENTER>



<P>Perl, with the help of CGI.pm or the WWW libraries, allows complete control over



every aspect of the client/server HTTP transaction. Using the CGI::Request WWW library



in your scripts to manage and process variables passed between the client and the



server during a transaction makes your code more secure, easier to maintain, and



easier to write.



<CENTER>



<H4><A NAME="Heading15"></A><FONT COLOR="#000077">CGI.pm versus Individual libwww



Modules</FONT></H4>



</CENTER>



<P>CGI.pm and the modular WWW libraries are very similar in function but very different



in purpose. CGI.pm includes all of the functionality of the individual modules, and



in most cases, more. In fact, Lincoln Stein, the author of CGI.pm and the WWW modules,



developed the CGI.pm module in parallel with the CGI, URL, and HTML modules. The



individual modules are designed to provide a very optimized solution to about nine



different aspects of programming with Perl for the Web. Modules exist for the Web



functions listed in Table 5.4. <TT>Table 5.4. Function of libwww modules. </TT>



<TABLE BORDER="0">



	<TR ALIGN="LEFT" rowspan="1">



		<TD ALIGN="LEFT"><B>Module</B></TD>



		<TD ALIGN="LEFT"><B>Function</B></TD>



	</TR>



	<TR ALIGN="LEFT" rowspan="1">



		<TD ALIGN="LEFT">CGI::</TD>



		<TD ALIGN="LEFT">Allows control over HTTP request and response headers. Mini HTTP server, CGI-based



			imagemapping, &quot;smart&quot; forms.</TD>



	</TR>



	<TR ALIGN="LEFT" rowspan="1">



		<TD ALIGN="LEFT">File::</TD>



		<TD ALIGN="LEFT">Parsing of directory listings and a persistent counter.</TD>



	</TR>



	<TR ALIGN="LEFT" rowspan="1">



		<TD ALIGN="LEFT">Font::</TD>



		<TD ALIGN="LEFT">Interface to Adobe Font Metrics Files.</TD>



	</TR>



	<TR ALIGN="LEFT" rowspan="1">



		<TD ALIGN="LEFT">HTML::</TD>



		<TD ALIGN="LEFT">Creation and parsing of HTML elements. Format HTML as text or PostScript.</TD>



	</TR>



	<TR ALIGN="LEFT" rowspan="1">



		<TD ALIGN="LEFT">HTTP::</TD>



		<TD ALIGN="LEFT">Generation of HTTP requests, responses, and headers. Content negotiation and status



			code processing.</TD>



	</TR>



	<TR ALIGN="LEFT" rowspan="1">



		<TD ALIGN="LEFT">LWP::</TD>



		<TD ALIGN="LEFT">Low-level I/O, socket interface, file type recognition.</TD>



	</TR>



	<TR ALIGN="LEFT" rowspan="1">



		<TD ALIGN="LEFT">MIME::</TD>



		<TD ALIGN="LEFT">Encoding and decoding Base64 and QP.</TD>



	</TR>



	<TR ALIGN="LEFT" rowspan="1">



		<TD ALIGN="LEFT">URI::</TD>



		<TD ALIGN="LEFT">Filtering, processing of URIs, elimination of unsafe characters in strings.</TD>



	</TR>



	<TR ALIGN="LEFT" rowspan="1">



		<TD ALIGN="LEFT">WWW::</TD>



		<TD ALIGN="LEFT">Parse robot.txt files.</TD>



	</TR>



</TABLE>



Think of each of the individual modules as a very focused and optimized solution



to a specific task. Most of the time, you will be writing scripts that use features



from more than one of the preceding modules. If you have to load any more than three



modules, it is actually more efficient to just load the entire CGI.pm. Loading each



of the modules separately actually is more resource intensive due to the overhead



associated with loading multiple files. The CGI.pm module also has a more robust



feature set than the individual modules.</P>



<P>Anything your CGI prints to STDOUT gets sent back to the Web server and out to



the browser, unless you are using nph-CGIs, in which case, STDOUT goes directly to



the browser without being parsed by the server. Traditionally, before Perl5 and CGI.pm,



HTML forms were created by CGI scripts by printing the HTML form elements to STDOUT.



Traditionally, the name/value pairs would have to be URI-decoded from either the



URI (<TT>GET</TT> method) or STDIN (<TT>POST</TT> method). Back in the days of Perl



4, include scripts such as cgi-lib.pl were written to deal with these nuisances.



However, there were still problems. Using cgi-lib.pl, the state of a form (the <BR>



contents of the variables) was lost from invocation to invocation because neither



the browser nor the server retained data from a previous transaction, debugging was



difficult, namespace was trampled, and the code was messy and would emit errors before



the HTTP response headers were printed if Perl was run in strict mode.







<DL>



	<DT></DT>



</DL>







<CENTER>



<H3><A NAME="Heading16"></A><FONT COLOR="#000077">URL:</FONT></H3>



</CENTER>











<BLOCKQUOTE>



	<P>If you still aren't convinced, a complete rundown on all of the benefits of using



	CGI.pm and Perl5 over the old methods such as cgi-lib.pl can be found at the following



	URL:







</BLOCKQUOTE>







<PRE><FONT COLOR="#0066FF">http://perl.com/perl/info/www/!cgi-lib.html



</FONT></PRE>



<P>CGI.pm was created to address these problems and build a wide and extensible bridge



between your CGI Perl script and the CGI interface. With CGI.pm, HTML forms can be



generated easily by making a few Perl function calls. When you use CGI.pm to generate



HTML forms, the values of the input fields from previous queries (if there were any)



are automatically used to initialize the form, so the state of the form is preserved



from invocation to invocation. URI encoding and <BR>



decoding of special characters is handled transparently by CGI.pm. <TT>GET</TT> and



<TT>POST</TT> methods are handled appropriately. If your script is run from the UNIX



shell, the option of inputting variables from the command line is given. In short,



CGI.pm takes care of almost all of the gory details of CGI and lets you focus on



writing your program.</P>



<P>The easiest way to learn to use CGI.pm or any of the modules is to simply start



using them in your scripts.



<CENTER>



<H3><A NAME="Heading17"></A><FONT COLOR="#000077">Example 1: Creating Forms with



CGI.pm</FONT></H3>



</CENTER>



<P>The example in Listing 5.1 introduces the usage of CGI.pm in a typical script



that generates and displays an HTML form. When run, the script parses the input from



<TT>GET</TT> and <TT>POST</TT> methods, then prints the HTML output for a Web page



with a form on it. If the script finds any data in the input stream (a previously



filled-out form), it will be printed at the bottom of the page. The output is shown



in Figure 5.3.







<DL>



	<DT></DT>



</DL>







<CENTER>



<H3>



<HR WIDTH="83%">



<BR>



<FONT COLOR="#000077">NOTE:</FONT></H3>



</CENTER>











<BLOCKQUOTE>



	<P>The line numbers in Listing 5.1 are for reference only and are not to be typed



	in.<BR>



	



<HR>











</BLOCKQUOTE>







<CENTER>



<H3><A NAME="Heading19"></A><FONT COLOR="#000077">Listing 5.1. Using CGI.pm with



forms</FONT></H3>



</CENTER>



<PRE><FONT COLOR="#0066FF">1   #!/usr/local/bin/perl



2   use CGI;



3   $query = new CGI;



4



5   print $query-&gt;header;



6



7   print $query-&gt;start_html(&quot;Using CGI.pm with Forms&quot;);



8   print &quot;&lt;H2&gt; Example 1: Using CGI.pm with Forms&lt;/H2&gt;



9         &lt;HR&gt;\n&quot;;



10



11  &amp;print_prompt($query);



12  &amp;do_work($query);



13  print $query-&gt;end_html;



14



15  # Subroutines ###



16



17  sub print_prompt {



18  my($query) = @_;



19  print $query-&gt;start_multipart_form;



20



21  print &quot;



22  &lt;table border=0 cellpadding=0 cellspacing=0 width=550&gt;



23  &lt;tr&gt;&lt;td&gt;&quot;,



24  $query-&gt;textfield(-name=&gt;`first_name',



25                    -default=&gt;`John',



26                    -size=&gt;25),



27  &quot;&lt;BR&gt;First Name&lt;/td&gt;&lt;td&gt;&quot;,



28  $query-&gt;textfield(-name=&gt;`mi', -size=&gt;2, -default=&gt;`A'),



29  &quot;&lt;BR&gt;M.I.&lt;/td&gt;&lt;td&gt;&quot;,



30  $query-&gt;textfield(-name=&gt;`last_name',



31                    -default=&gt;`Doe',



32                    -size=&gt;25),



33  &quot;&lt;BR&gt;Last Name&lt;/tr&gt;&lt;/td&gt;



34  &lt;tr&gt;&lt;td colspan=3&gt;&quot;,



35  $query-&gt;textfield(-name=&gt;`address', -size=&gt;55),



36  &quot;&lt;BR&gt;Street Address&lt;/td&gt;&lt;/tr&gt;



37  &lt;tr&gt;&lt;td&gt;&quot;,



38  $query-&gt;textfield(-name=&gt;`city',



39                    -size=&gt;25),



40  &quot;&lt;BR&gt;City&lt;/td&gt;&lt;td&gt;&quot;,



41  $query-&gt;textfield(-name=&gt;`state', -size=&gt;2),



42  &quot;&lt;BR&gt;State&lt;/td&gt;&lt;td&gt;&quot;,



43  $query-&gt;textfield(-name=&gt;`zip',



44                    -size=&gt;10),



45  &quot;&lt;BR&gt;Zip Code&lt;/tr&gt;&lt;/td&gt;



46   &lt;/table&gt;&quot;;



47



48  print &quot;&lt;HR&gt;&lt;table border=0 cellspacing=0



49              cellpadding=0 width=550&gt;



50        &lt;tr&gt;&lt;td&gt;&lt;EM&gt;What Opera

⌨️ 快捷键说明

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