Steps in order to pass data from HTML form to Perl script

john john picture john john · Aug 5, 2012 · Viewed 12.6k times · Source

I have created a simple HTML, which contains the form below:

<form action="WEB-INF/cgi/run.pl" method="post">
      <table border="0" cellspacing="0"> 

      <tbody>
      <tr><th align="center" bgcolor="F7F5F2"> <p class="normal">Submission Form</p> </th></tr>

      <tr><td align="center" bgcolor="F7F5F2"> <p class="normal">Insert your text below:</p> </td></tr>
      <tr><td><textarea wrap="virtual" name="seq_data" rows="15" cols="80"></textarea></td></tr>  
    </tbody></table>
    or upload a file :   <input type="file" name="file" size="29" border="0"><br><br>
    <input class="normalc" value="Submit Query" type="submit">
    <input class="normalc" value="Clear Form" type="reset"><p></p>
</form>

I need to pass the data from the form as input to a perl script (run.pl).

While searching the internet I have read that: 1) I need to test my website through apache tomcat. I have installed apache version 7.0 and modified the Tomcat 7.0/conf/web.xml file by removing the XML comments from around this servlet:

<servlet>
    <servlet-name>default</servlet-name>
    <servlet-class>org.apache.catalina.servlets.DefaultServlet</servlet-class>
    <init-param>
        <param-name>debug</param-name>
        <param-value>0</param-value>
    </init-param>
    <init-param>
        <param-name>listings</param-name>
        <param-value>false</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

I have also created the directory "WEB-INF/cgi" in which I placed my perl script.

2) I need to modify my Perl script, but I can't find out what I should add in order to pass the data from the html form to my script.

I don't know if there are any other necessary steps other than modifying Tomcat and Perl script. I have read many relevant topics, but I still can't find a step by step guide. Please help.

Answer

Borodin picture Borodin · Aug 6, 2012

When a web server receives an HTTP request it generally responds with the contents of the resource. However if the URL specifies a Common Gateway Interface (CGI) resource it will run it and return the output of the program instead.

The server's configuration specifies the distinction between CGI and non-CGI resources, and this can be be based either on the file extension - .cgi, .pl etc. - or on where the file is in the server's directory structure.

The server passes on the information in the HTTP request to the CGI program through its STDIN and also the environment variables of the process. In general the parameters for a PUT or POST request will appear in STDIN while those for a GET request are inserted into the environment variables.

The program's job is to build the required response based on these parameters and print them to STDOUT. It may also make use of database information and other system information. This output will be used by the server as the content of the HTTP response.

You should look at the Perl CGI module which wraps this interface in convenient subroutines.