Coding of CGI data

Source: Internet
Author: User
Tags html page mail trim

In this release, we will collect names and e-mail addresses and save them to a file in the following form:
<email@domain.com>;
This is a very convenient format for any e-mail program. There is no easy way to do this because you only need to collect two fields and the CGI uses a special format for the encoding in the field. If you start a raw HTML page and add the following line of code, you can correctly understand this:

<form method= "Get" action= "/cgi-bin/listmgr2.exe" >
<p>name: <input TYPE = "text" name = "Name" 
Value = "" size = "></p> <p>email address
: <input TYPE =" text " 
NAME =" Email "VALUE =" siz E = "+" ></p>
<p><input type = "Submit" name = "Submit" > </p>
</Form>


The above code creates two data entry fields (extents), named name and email. There is also a submit button for collecting data and sending it to a CGI program. Listmgr2.exe is an executable file that resides in a special program directory. On our web server, this directory is generally called "cgi-bin" (note ③). If the program is not found in that directory, the result will not appear. Fill out this form and press the submit button to see something like the following in the browser's URL address window:
http://www.myhome.com/cgi-bin/listmgr2.exe?name=first+last& Email=email@domain.com&submit=submit

③: Under the WINDOWS32 platform, Microsoft Office 97 or other products are available Personal Web Server (Microsoft Personal Web Server) for testing. This is the best way to do this because you do not have to formally connect to the network to complete the test in a local environment (very fast). If you are using a different platform, or you do not have a product like Office 97 or FrontPage 98, go online and find a free Web server for yourself to test.

Of course, the above URL will not be split when it actually appears. You can see how to encode and pass data to a CGI. At least one thing is certain-a space is not allowed (because it is often used to separate command-line arguments). All the required spaces are replaced with the "+" number, each containing the field name (which is determined by the HTML page) followed by a "=" number and the formal field data, ending with a "&".
By then, you may have doubts about the use of "+", "=" and "&". If you have to use these characters in a field, how do you declare them? For example, we might use the name "John & Marshasmith", where "&" stands for "and". In fact, it will encode the following:
John+%26+marsha+smith
In other words, the special character is converted to a "%" and followed by its hexadecimal ASCII encoding.
Fortunately, Java has a tool to help us with this encoding. This is a static method of the Urlencoder class, named Encode (). Use the following procedure to experiment with this method:

 

: Encodedemo.java
//demonstration of Urlencoder.encode ()
import java.net.*;

public class Encodedemo {public
  static void Main (string[] args) {
    String s = "";
    for (int i = 0; i < args.length i++)
      s + + = Args[i] + "";
    s = Urlencoder.encode (S.trim ());
    System.out.println (s);
  }
///:~

The program takes some command-line arguments and merges them into a string of multiple words separated by a space (the last space is String.Trim ()). They are then encoded and printed.
To invoke a CGI program, all that a piece of the program does is collect data from its own fields or other places, encode all the data into the correct URL style, and then assemble it into a single string. Each field name is followed by an "=" symbol, followed by the official data, followed by a "&". To build the complete CGI command, we put this string at the URL of the CGI program and a "?" After. This is the standard method of calling all CGI programs. As you'll soon see, it's easy to use a piece of code to complete all of these encodings and mergers.

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.