HowTo JAVA-CGI

Source: Internet
Author: User

 

1. Introduction
The Java design method does not allow programmers to easily obtain system environment variables. as established by the Java Development Tool (JDK), calling a program must use multiple tokens, which is difficult to work with standard HTML forms/CGI operations. there are some ways to overcome these limitations, and I have implemented one of them. for more information, see the following.

1.1. Prerequisites
I suppose you have a general knowledge of the concept of HTML and CGI, and you know the most basic knowledge about your HTTP server. You should also know how to write Java programs. Otherwise, these are meaningless.

1.2. This document
The latest version of this article can be found from: http://www.orbits.com/software/Java_CGI.html.

1.3. software suite
The latest version of the software suite mentioned in this article can be obtained by anonymous FTP ftp://ftp.orbits.com/pub/software/java_cgi-0.4.tgz. This suite contains the original SGML file in this article.

The distribution of these suites must be based on the GNU Library General Public License. This article is based on the Linux HOWTO copyright statement.

If you use this software, refer to http://www.orbits.com/software/java_cgi.html to help others find the type of Java CGI.

2. Set the server to execute the Java CGI program (Interpretation)
This section teaches you how to install my Java CGI suite and explains a lot, so that you can understand what results your behavior will lead. if you only want to install the program and do not want to know why, you can directly jump to the set server to execute the Java CGI program (briefly ).

2.1. System Requirements
This software should be able to be installed on any UNIX-like web servers that have installed Java program development tools. I installed it on the Debian Linux system that runs the apache server. if you find that it cannot be stunned, is it the norm of the European Union? Dhs@orbits.com.

Unfortunately, the interpreter seems to be a memory-consuming monster during Java execution-if you use many Java CGI programs, you may lose several MB of RAM to your server.

2.2. Java CGI auxiliary software
The secondary software I wrote is called Java CGI. You can get it from the ftp://www.orbits.com/pub/software/java_cgi-0.4.tgz (the version number may change ).

2.3. Unlock original files
Find a suitable directory to unbind the suite. (If you do not have a standard place for the software, I suggest you put it in/usr/local/src.) Use this command to unbind the suite:

Gzip-dc java_cgi-0.4.tgz | tar-xvf-

This will generate a directory named java_cgi-0.4, where you can find the file mentioned elsewhere in this article. (If the version number changes, switch to the one in that suite .)

2.4. Determine your local Directory Policy
You must decide to let your Java CGI program live there. in general, you will want to put it in a directory parallel to your cgi-bin. my apache server sets/var/web/cgi-bin as the cgi-bin directory, so I use/var/web/javacgi as the place where the Java CGI program is stored. you may not want to put your Java CGI program into an existing CLASSPATH directory. edit Makefile to reflect your system configuration. are you sure you want to use root to check in and then execute make install. this will compile the Java program and modify the java. cgi Command to match your system and install the program in the appropriate location. if you want to have the HTML version of this article and an HTML test file, use make all.

2.5. Test your installation
The HTML files installed in this suite are called javacgitest.html, javaemailtest.html, and javahtmltest.html. if you use make all in the previous section, they will be placed in the WEBDIR directory you specified in Makefile. if not, run make test to create them from javacgitest.html-dist, javaemailtest.html-dist, and javahtmltest.html-dist.

After you confirm that your installation works properly, you may want to remove the CGI_Test, Email_Test, and HTML_Test categories from your JAVACGI directory, and remove javacgitest.html from the WEBDIR directory, javaemailtest.html, and javahtmltest.html, because they will display User information that should only be viewed by the Server Manager.

3. Set the server to execute the Java CGI program (briefly)

Retrieve the Java CGI kit from ftp://www.orbits.com/pub/software/java_cgi-0.4.tgz. (the version number may change .)
Use this command to unlock the kit:
Gzip-dc java_cgi-0.4.tgz | tar-xvf-

(If the version number is changed, use the one in the suite .)
Modify the Makefile in the newly generated java_cgi-0.4 directory to match your system.
Run make install as root. this will compile the Java program, add the specific information of your system, and install many files. if you want to have the HTML version of this article and an HTML test file, use make all.
Then you can use it.
4. Run a Java CGI program
4.1. Obstacles to Java program execution in CGI Mode
There are two main problems with running Java programs on the web server:

4.1.1. You cannot execute Java programs like normal execution files
You must run the Java runtime interpreter and provide the initial class (required for program execution) as a command column ). in an HTML form, there is no way to provide command columns to the web server.

4.1.2. There is no general method for Java programs to obtain environment variables.
Every environment variable required by the Java program must be passed in. (In the Java program) There is no getenv () function similar to the C language.

4.2. execute Java CGI to solve the problem
To deal with these difficulties, I wrote a CGI Command draft program to provide the information required by the Java interpreter.

4.2.1. java. cgi Command draft
This command draft manages the interaction between the HTTP server and the Java CGI program you want to use. it extracts the name of the program you want to run from the information provided by the server. it uploads all environment variable data to a temporary archive. then, it adds the file and program name to the command column to execute the Java interpreter at the execution time.

The setting and installation of the java. cgi Command draft are described in the section determining your local directory policy.

4.2.2. Raise java. cgi from HTML form
Use the HTML form of the Java CGI program to specify its behavior in the following ways:

<Form action = "/cgi-bin/java. cgi/CGI_Test" method = "POST">

Here,/cgi-bin/is the directory of your local CGI binary execution file, java. cgi is the front end that allows us to execute Java programs from the web, and CGI_Test is an example of the name of the Java program to be executed.

5. Use Java CGI categories
Currently, three main categories are supported: CGI, Email, and HTML. I am considering adding the class for processing Input and Output in MIME format-MIMEin & MIMEout.

There are also some categories for support and testing. CGI_Test, Email_Test, and HTML_Test are used to test your installation. you can also use it as the starting point for using these category libraries in your program. the Text category is the base category of Email and HTML.

5.1. CGI
5.1.1. Category syntax
Public class CGI

5.1.2. Category Description
CGI information held by the CGI category-the environment variable set by the web server and the name/value sent by the form when the submit is pressed. all information is stored in the category object Properties.

This category is in ''orbits. net package.

5.1.3. Member Summary

--------------------------------------------------------------------------------

CGI () // Constructor
GetNames () // get the name of the string Column
GetValue () // get the value of the specified name

--------------------------------------------------------------------------------

5.1.4. See

CGI_Test.

5.1.5. CGI ()

Purpose
Construct an object that contains the available CGI data

Syntax
Public CGI ()

Description
When a CGI object is created, all available CGI data is stored in the local storage space of the new object.

5.1.6. getNames ()

Purpose
Lists the names of defined values.

Syntax
Public Enumeration getNames ()

Description
Provides a complete list of names of all defined values.

Return
All Enumeration objects with names.

5.1.7. getValue ()

Purpose
Retrieves the value of the specified name.

Syntax
Public String getValue (String name)

Description
This method provides the correspondence between names and values sent by HTML form.

Parameters

Name
The selected key value.

Return
A String object containing the corresponding value.

5.2. CGI_Test
This category provides two functions: an example of how to use the CGI category and a test program used to determine that the Java CGI suite works normally.

5.2.1. Member Summary

--------------------------------------------------------------------------------

Main () // Program main ().


--------------------------------------------------------------------------------

5.2.2. See
CGI.

5.2.3. main ()

Purpose
Provides the main () method.

Syntax
Public static void main (String argv [])

Description
This is the entry of the CGI program, but returns a list of available name/value pairs and their present values.

Parameters

Argv []
Parameters passed in by the java. cgi Command draft. Not used currently.

5.3. Email
5.3.1. Category syntax
Public class Email extends Text

5.3.2. Category Description
A message is created by the add * () method of the Text Category and added to the email-specific method. After the message is completed, it is sent to its destination.

This category is in the ''Orbits. net package.

5.3.3. Member Summary

---------

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.