📄 fcgi-java.htm
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2//EN"><HTML> <HEAD> <TITLE> Integrating FastCGI with Java </TITLE><STYLE TYPE="text/css"> body { background-color: #FFFFFF; color: #000000; } :link { color: #cc0000 } :visited { color: #555555 } :active { color: #000011 } dt.c4 {font-style: italic} h5.c3 {text-align: center} p.c2 {text-align: center} div.c1 {text-align: center}</STYLE> </HEAD> <BODY> <DIV CLASS="c1"> <A HREF="http://fastcgi.com"><IMG BORDER="0" SRC="../images/fcgi-hd.gif" ALT="[[FastCGI]]"></A> </DIV> <BR CLEAR="all"> <DIV CLASS="c1"> <H3> Integrating FastCGI with Java </H3> </DIV> <!--Copyright (c) 1996 Open Market, Inc. --> <!--See the file "LICENSE.TERMS" for information on usage and redistribution--> <!--of this file, and for a DISCLAIMER OF ALL WARRANTIES. --> <!-- $Id: fcgi-java.htm,v 1.4 2002/02/25 00:42:59 robs Exp $ --> <P CLASS="c2"> Steve Harris<BR> Open Market, Inc.<BR> <EM>7 May 1996</EM> </P> <H5 CLASS="c3"> Copyright © 1996 Open Market, Inc. 245 First Street, Cambridge, MA 02142 U.S.A.<BR> Tel: 617-621-9500 Fax: 617-621-1703 URL: <A HREF= "http://www.openmarket.com/">http://www.openmarket.com/</A><BR> </H5> <HR> <H3> <A NAME="S1">1. Introduction</A> </H3> <P> Java is an object-oriented programming language developed by Sun Microsystems. The Java Depvelopers Kit (JDK), which contains the basic Java class packages, is available from Sun in both source and binary forms at Sun's <A HREF="http://java.sun.com/java.sun.com/JDK-1.0/index.html">JavaSoft</A> site. This document assumes that you have some familiarity with the basics of compiling and running Java programs. </P> <P> There are two kinds of applications built using Java. </P> <UL> <LI> <I>Java Applets</I> are graphical components which are run off HTML pages via the <TT><APPLET></TT> HTML extention tag.<BR> <BR> </LI> <LI> <I>Java Applications (Apps)</I> are stand-alone programs that are run by invoking the Java interpreter directly. Like C programs, they have a <TT>main()</TT> method which the interpreter uses as an entry point. </LI> </UL> <P> The initial emphasis on using Java for client side applets should not obscure the fact that Java is a full strength programming language which can be used to develop server side stand alone applications, including CGI and now FastCGI applications. </P> <P> The remainder of this document explains how to write and run FastCGI Java applications. It also illustrates the conversion of a sample Java CGI program to a FastCGI program. </P> <H3> <A NAME="S2">2. Writing FastCGI applications in Java</A> </H3> <P> Writing a FastCGI application in Java is as simple as writing one in C. </P> <OL> <LI> Import the <TT>FCGIInterface</TT> class. </LI> <LI> Perform one-time initialization at the top of the <TT>main()</TT> method. </LI> <LI> Create a new <TT>FCGIInterface</TT> object and send it an <TT>FCGIaccept()</TT> message in a loop. </LI> <LI> Put the per-request application code inside that loop. </LI> </OL> <P> On return from <TT>FCGIaccept()</TT> you can access the request's environment variables using <TT>System.getProperty</TT> and perform request-related I/O through the standard variables <TT>System.in</TT>, <TT>System.out</TT>, and <TT>System.err</TT>. </P> <P> To illustrate these points, the kit includes <TT>examples/TinyCGI</TT>, a CGI Java application, and <TT>examples/TinyFCGI</TT>, the FastCGI version of TinyCGI. These programs perform the same functions as the C programs <TT>examples/tiny-cgi.c</TT> and <TT>examples/tiny-fcgi.c</TT> that are used as examples in the <A HREF="fcgi-devel-kit.htm#S3.1.1">FastCGI Developer's Kit document</A>. </P> <H4> A. TinyCGI </H4><PRE> class TinyCGI { public static void main (String args[]) { int count = 0; ++count; System.out.println("Content-type: text/html\n\n"); System.out.println("<html>"); System.out.println( "<head><TITLE>CGI Hello</TITLE></head>"); System.out.println("<body>"); System.out.println("<H3>CGI-Hello</H3>"); System.out.println("request number " + count + " running on host " + System.getProperty<"SERVER_NAME")); System.out.println("</body>"); System.out.println("</html>"); } }</PRE> <H4> B. TinyFCGI </H4><PRE> import FCGIInterface;class TinyFCGI { public static void main (String args[]) { int count = 0; while(new FCGIInterface().FCGIaccept()>= 0) { count ++; System.out.println("Content-type: text/html\n\n"); System.out.println("<html>"); System.out.println( "<head><TITLE>FastCGI-Hello Java stdio</TITLE></head>"); System.out.println("<body>"); System.out.println("<H3>FastCGI-HelloJava stdio</H3>"); System.out.println("request number " + count + " running on host " + System.getProperty<"SERVER_NAME")); System.out.println("</body>"); System.out.println("</html>"); } } }</PRE> <H4> C. Running these Examples </H4> <P> We assume that you have downloaded the JDK and the FastCGI Developer's Kit, and that you have a Web server running that can access the <TT>fcgi-devel-kit/examples</TT> directory. In all cases where we specify paths, we are using relative paths within <TT>fcgi-devel-kit</TT> or the JDK which will need to be enlarged to a full path by the user. </P> <H5> Configuring </H5> <OL> <LI> Add your JDK's <TT>java/bin</TT> directory to your Unix <TT>PATH</TT> if it isn't there already.<BR> <BR> </LI> <LI> Add the directories <TT>fcgi-devel-kit/examples</TT> and <TT>fcgi-devel-kit/java/classes</TT> to your Java <TT>CLASSPATH</TT>.<BR> <BR> </LI> <LI> In your Open Market Secure WebServer configuration file, <TT>httpd.config</TT>, add the following two lines:<BR> <BR> <TT>ExternalAppClass TinyFCGI -host</TT> <I>hostName:portNum</I><BR> <TT>Responder TinyFCGI fcgi-devel-kit/examples/TinyFCGI</TT><BR> <BR> <UL> <LI> <I>hostName</I> is the name of your host machine.<BR> </LI> <LI> <I>portNum</I> is the port that you've selected for communication between the Web server and the Java application.<BR> </LI> </UL> <BR> On other servers you can use <TT>cgi-fcgi</TT> to get a similar effect. </LI> <LI> Create a soft link <TT>examples/javexe</TT> to the <TT>java/bin</TT> directory in your JDK. This link is required only to run the CGI scripts <TT>examples/TinyCGI.cgi</TT> and <TT>examples/TinyFCGI.cgi</TT>, which use it to invoke the Java interpreter <TT>java/bin/java</TT>. It is not used by FastCGI applications. </LI> <LI> You might have to modify <TT>examples/TinyFCGI.cgi</TT> to use a Unix shell for which your CLASSPATH is defined. </LI> </OL> <H5> Running </H5> <UL> <LI> To run TinyFCGI as FastCGI, you invoke the Java interpreter with the -D option, giving it the <TT>FCGI_PORT</TT> environment variable and the same <I>portNum</I> that was used in the Web server configuration. The command is:<BR> <BR> <TT>java -DFCGI_PORT=portNum TinyFCGI</TT><BR> <BR> Then point your browser at <TT>fcgi-devel-kit/examples/TinyFCGI</TT>. Notice that each time you reload, the count increments.<BR> <BR> </LI> <LI> To run TinyCGI, point your browser at <TT>fcgi-devel-kit/examples/TinyCGI.cgi</TT> on your host machine. Notice that the count does not increment.<BR> <BR> </LI> <LI> Finally, you can run TinyFCGI as a straight CGI program by pointing your browser at <TT>fcgi-devel-kit/examplesi/TinyFCGI.cgi.</TT> The results are exactly the same as when you ran TinyCGI. Invoking a FastCGI program without an <TT>FCGI_PORT</TT> parameter tells the FastCGI interface to leave the normal CGI environment in place. </LI> </UL> <P> Due to gaps in the Java interpreter's support for listening sockets, Java FastCGI applications are currently limited to being started as external applications. They can't be started and managed by the Web server because they are incapable of using a listening socket that the Web server creates. </P> <H3> <A NAME="S3">3. Standard I/O and Application Libraries</A> </H3> <P> As we have seen above, FastCGI for Java offers a redefinition of standard I/O corresponding to the the <I>fcgi_stdio</I> functionality. It also offers a set of directly callable I/O methods corresponding to the
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -