2012-08-05 47 views
1

我已经创建了一个简单的HTML,它包含下面的表格:步骤,以便从HTML形式的数据传递给Perl脚本

<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> 

我需要从形式作为输入数据传递到一个perl脚本(run.pl)。

在搜索互联网时,我读到: 1)我需要通过apache tomcat测试我的网站。我已经安装了Apache 7.0版本,并通过周围这个servlet删除XML注释修改Tomcat的7.0/conf目录/ web.xml文件:

<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> 

我也创建在目录“WEB-INF/CGI”我放置了我的perl脚本。

2)我需要修改我的Perl脚本,但我无法找到我应该添加什么,以便将数据从html表单传递到我的脚本。

我不知道除修改Tomcat和Perl脚本外是否还有其他必要的步骤。 我已阅读了许多相关主题,但仍无法找到一步一步的指南。请帮忙。

回答

4

当Web服务器收到HTTP请求时,它通常会响应资源的内容。但是,如果URL指定一个Common Gateway Interface(CGI)资源,它将运行它并返回程序的输出。

服务器的配置指定了CGI和非CGI资源之间的区别,这可以基于文件扩展名.cgi,.pl等 - 或文件在服务器目录结构中的位置。

服务器将HTTP请求中的信息通过其STDIN以及进程的环境变量传递给CGI程序。通常,PUTPOST请求的参数将出现在STDIN中,而GET请求的参数则插入到环境变量中。

程序的工作是根据这些参数构建所需的响应并将它们打印到STDOUT。它也可能利用数据库信息和其他系统信息。该输出将被服务器用作HTTP响应的内容。

你应该看看Perl CGI module,它将这个接口包装在方便的子程序中。

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/>"; 
} 

你可以问我,如果你不知道如何从数组在Perl中读取数值,但首先试着理解我发布的这个例子,然后我会帮助你。

+0

密切关注此:** enctype =“multipart/form-data”**,因为我没有注意到这么多时间:) – GLES 2012-08-09 20:09:57

+0

感谢您的回答。对不起,这么晚回复,但我在国外。 – 2012-08-26 11:50:27

+0

就enctype =“multipart/form-data”而言,我明白我应该将其添加到我的表单中,因为它允许上传整个文件。 我研究了你的代码并试图运行它。所以我在我的Apache目录中创建了一个新文件夹,并将您的代码粘贴到两个单独的文件(evaluate.pl和application.html)中。我只是将第一个perl脚本行更改为:#!C:\ Perl64 \ bin \ perl.exe。我通过firefox运行它,你的html显得很好。但是当我按提交时,我看到的是你的Perl代码(我也有同样的问题)。 – 2012-08-26 12:21:06

相关问题