2012-08-05 43 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

相關問題