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

白昼怎懂夜的黑 提交于 2019-12-24 01:44:41

问题


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.


回答1:


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.




回答2:


application.html

<!DOCTYPE html>
<html lang="en">
<head>
  <meta http-equiv="content-type" content="text/html; charset=utf-8">
  <title>
    Application| Form
  </title>
  <style>
  input
  {
    display:block;
  }
  </style>
</head>
  <body>
    <form action="evaluate.pl" method="post" enctype="multipart/form-data">
      <input type="file" name="photo">
      <input type="file" name="photo">
      <input type="text" name="email_id" placeholder="email id">
      <input type="submit" value="submit">
    </form>
  </body>
</html>

evaluate.pl

#!C:/wamp/bin/perl/bin/perl.exe
#Purpose: To find the number of photos uploaded

use CGI;
use strict;
my $cgi = new CGI;

print "Content-Type:text/html\r\n\r\n";

my $param = $cgi->{param};

foreach( keys(%{$param}) ){
  print $_," -> ",$param->{$_};
  print "<br/>";
}

You can ask me if you do not know how to read values from arrays in perl, but first try to understand this example that I have posted and then I will help you.



来源:https://stackoverflow.com/questions/11817809/steps-in-order-to-pass-data-from-html-form-to-perl-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!