我正在尝试使用php脚本运行Java程序。
首先,php显示一个表单,用户可以在其中输入两个值:价格和销售税率。接下来,它提取值并将其传递给Java程序(预编译为.class文件)。
我不确定是否在所有Java代码正常工作的情况下将输出打印在了哪里。我的最终目标是在html页面中向用户显示结果。
我将文件内容上传到Web服务器,并尝试从那里运行它。
更新:
如何使用shell_exec或exec运行Java代码?我需要将参数(价格,salesTax)传递给shell_exec。返回的输出存储在哪里?
PHP代码:
> <?php
>
> $salesTaxForm = <<
>
>
>
> Price (ex. 42.56):
>
>
> value="">
>
> Sales Tax rate (ex. 0.06):
>
>
> value="">
>
>
>
>
>
> 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;
}
}