[英]Run CGI Java Program
我想使用Apache HTTP Server運行Java程序。 我正在努力在瀏覽器中顯示簡單的輸出。
在我的cgi-bin文件夾中,有CgiTest.java,看起來像這樣:
#!"C:\Program Files\Java\jdk1.8.0_101\bin\java.exe"
public class CgiTest {
public static void main(String[] args) {
String type = "Content-Type: text/html\n\n";
String output = "<html>" +
"<p>Hi there </p>" +
"</html>";
System.out.println(type);
System.out.println(output);
}
}
我得到的錯誤是:
End of script output before headers: CgiTest.java
Error: Could not find or load main class C:.Apache24.cgi-bin.CgiTest.java\r: C:/Apache24/cgi-bin/CgiTest.java
如何運行我的Java程序? 如果必須從某些腳本中調用它,該怎么做?
我成功了! 對於那些發現這一天的人來說,這是一種方法:
步驟1. Java程序
public class CgiTest {
public static void main(String[] args) {
String type = "Content-Type: text/html\n\n";
String output = "<html>" +
"<p>Hi there </p>" +
"</html>";
System.out.println(type);
System.out.println(output);
}
}
步驟2.將其放在cgi-bin文件夾中
步驟3.運行javac CgiTest.java
步驟4.安裝cygwin
步驟5.在cgi-bin文件夾中創建invoker.cgi:
#!C:\cygwin64\bin\bash.exe
java -cp ./ CgiTest
步驟6.配置conf / httpd.conf並允許執行cgi腳本。 確保您有以下幾行:
<Directory "c:/Apache24/cgi-bin">
Options +ExecCGI
AddHandler cgi-script .cgi .pl
Options FollowSymLinks
Require all granted
</Directory>
AddHandler cgi-script .cgi
步驟7.在瀏覽器中運行URL。 確保正確輸入端口。 我的是180但你的在conf / httpd.conf中
http://localhost:180/cgi-bin/invoker.cgi
這是在Windows中執行此操作的方法之一。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.