我正在尝试使用 php 脚本运行 java 程序。
首先,php 显示一个表单,用户在其中输入两个值:价格和销售税率。接下来,它提取值并将其传递给 java 程序(预编译为 .class 文件)。
如果所有 java 代码都在工作,我不确定在哪里打印输出。我的最终目标是在 html 页面中向用户显示结果。
我将文件内容上传到我的网络服务器并尝试从那里运行它。
更新:
如何使用 shell_exec 或 exec 运行 java 代码?我需要将参数(价格、销售税)传递给 shell_exec。返回的输出存储在哪里?
PHP代码:
> <?php
> 
> $salesTaxForm = <<<SalesTaxForm
> 
> <form action="SalesTaxInterface.php" method="post">
> 
>    Price (ex. 42.56):<br>
> 
>    <input type="text" name="price" size="15" maxlength="15"
> value=""><br>
> 
>    Sales Tax rate (ex. 0.06):<br>
> 
>    <input type="text" name="tax" size="15" maxlength="15"
> value=""><br>
> 
>    <input type="submit" name="submit" value="Calculate!">
> 
>    </form>
> 
> SalesTaxForm;
> 
> if (! isset($submit)) :
> 
>    echo $salesTaxForm;
> 
> else :    $salesTax = new Java("SalesTax");
> 
>    $price = (double) $price;    $tax = (double) $tax;
> 
>    print $salesTax->SalesTax($price, $tax);
> 
> endif;
> 
> ?>
Java 代码:
import java.util.*; import java.text.*; public class SalesTax { public String SalesTax(double price, double salesTax) { double tax = price * salesTax; NumberFormat numberFormatter; numberFormatter = NumberFormat.getCurrencyInstance(); String priceOut = numberFormatter.format(price); String taxOut = numberFormatter.format(tax); numberFormatter = NumberFormat.getPercentInstance(); String salesTaxOut = numberFormatter.format(salesTax); String str = "A sales Tax of " + salesTaxOut + " on " + priceOut + " equals " + taxOut + "."; return str; } }