如何使用php运行java代码(.class)并显示在同一个网页上 [英] How to run java code (.class) using php and display on the same web page

查看:167
本文介绍了如何使用php运行java代码(.class)并显示在同一个网页上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



首先,php显示一个表单,用户输入两个值:价格和销售税率。
接下来,它提取这些值并将它传递给一个java程序(预编译为.class文件)。



我不确定输出到哪里如果在所有的Java代码正在工作。我的最终目标是在HTML页面中将结果显示给用户。



我将文件内容上传到我的Web服务器并尝试从此处运行。

update:



如何使用shell_exec或exec来运行java代码?我需要将参数(price,salesTax)传递给shell_exec。

PHP代码:

 > <?php 
>
> $ salesTaxForm =<<<< SalesTaxForm>
>
> < form action =SalesTaxInterface.phpmethod =post>
>
>价格(例如42.56):< br>
>
> < input type =textname =pricesize =15maxlength =15
>值= ><峰; br>
>
>销售税率(例如0.06):< br>
>
> < input type =textname =taxsize =15maxlength =15
>值= ><峰; br>
>
> < input type =submitname =submitvalue =Calculate!>
>
> < /形式>
>
> SalesTaxForm;
>
> if(!isset($ submit)):
>
> echo $ salesTaxForm;
>
>其他:$ salesTax = new Java(SalesTax);
>
> $ price =(double)$ price; $税=(双)$税;
>
> print $ salesTax-> SalesTax($ price,$ tax);
>
>万一;
>
> ?>

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

字符串taxOut = numberFormatter.format(tax);

numberFormatter = NumberFormat.getPercentInstance();

字符串salesTaxOut = numberFormatter.format(salesTax);

String str =销售税+ salesTaxOut +

on+ priceOut +等于+ taxOut +。;

return str;





$ / $ p

shell-exec执行您传递给它的命令。要使用它,你必须在你的类中添加一个Main方法,并且在命令行中传递像参数这样的属性,所以最后应该看起来像这样:



这是你必须在php上执行的代码

  $ output = shell_exec('java SalesTax 10.0 20.0'); 

其中 SalesTax 是您的java类, 10.0 是第一个参数, 20.0 秒。



您的主要方法应该是这样的

  public static void main(String args []){
double price = Double.valueOf(args [0]);
double salesTax = Double.valueOf(args [1]);
String output = SalesTax(price,salesTax);
System.out.println(输出);



$ b $ p
$ b

这是一个非常简单的实现,你应该添加验证和其他一些东西,但我认为这是主要想法。也许它应该更容易将其移植到php。



我希望你觉得这有帮助。 :)

I am trying to run a java program using php script.

First, php displays a form where users input two values: Price and Sales Tax Rate. Next, it extracts the values and passes it to a java program (precompiled as .class file).

I am uncertain as to where the output is printed if at all the java code is working. My ultimate goal is to display the result to the user in an html page.

I upload my file contents to my web server and try to run it from there.

update:

How can I use shell_exec or exec to run java code? I need to pass the parameters (price, salesTax) to shell_exec. Where is the returned output stored?

PHP code:

> <?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 Code:

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;

    }

}

解决方案

shell-exec executes the comand that you pass to it. To use this, you have to add a Main method to your class, and pass the properties like arguments in the comand line, so at the end it should look like this:

This is code that you have to execute on php

  $output = shell_exec('java SalesTax 10.0 20.0');

Where SalesTax is your java class, 10.0 is the first argument, and 20.0 the second.

Your main method should be something like this

public static void main(String args[]){
   double price = Double.valueOf(args[0]);
   double salesTax = Double.valueOf(args[1]);
   String output = SalesTax(price,salesTax);
   System.out.println(output);
}

It's a very simple implementation, you should still add validations and some other stuff, but I think that it's the main idea. Maybe it should be easier to just port it to php.

I hope that you find this helpful. :)

这篇关于如何使用php运行java代码(.class)并显示在同一个网页上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆