在网页上运行Java应用程序

时间:2011-12-22 15:28:18

标签: java html

我编写了一个我希望在网页中运行的Java应用程序。我该怎么做?

代码如下:

class Permutations {
    static long factorial(int num){
            long factorial = 1;
            for (int forBlockvar = num; forBlockvar > 1; forBlockvar--) {
                    factorial = factorial * forBlockvar;
            }
            return factorial;
    }

    public static void main(String[] args){
            long FactNmR;
            int n = 10;
            int num = n;
            int r = 4;
            int nMr = n - r;
            long FactN = factorial(num);
            if (nMr <= 1){
                    FactNmR = 1;
            }
            else  {
                    num = nMr;
                    FactNmR = factorial(num);
            }
            long permutations = FactN;
            permutations = permutations / FactNmR;
            System.out.println(permutations);
   }
}

2 个答案:

答案 0 :(得分:7)

使用java有几种方法可以做到这一点。

一种方法是使用Java Servlet。

您需要一个带有指向Servlet(扩展Java类)的操作的html表单 看看this tutorial

答案 1 :(得分:2)

也可以使用JSweet将Java编译为JavaScript,而不是运行服务器或applet。

这是JSweet对您的Permutations课程的翻译:

 /* Generated from Java with JSweet 2.0.0 - http://www.jsweet.org */
var Permutations = (function () {
    function Permutations() {
    }
    Permutations.factorial = function (num) {
        var factorial = 1;
        for (var forBlockvar = num; forBlockvar > 1; forBlockvar--) {
            factorial = factorial * forBlockvar;
        }
        ;
        return factorial;
    };
    Permutations.main = function (args) {
        var FactNmR;
        var n = 10;
        var num = n;
        var r = 4;
        var nMr = n - r;
        var FactN = Permutations.factorial(num);
        if (nMr <= 1) {
            FactNmR = 1;
        }
        else {
            num = nMr;
            FactNmR = Permutations.factorial(num);
        }
        var permutations = FactN;
        permutations = Math.floor(permutations / FactNmR);
        console.info(permutations);
    };
    return Permutations;
}());
Permutations["__class"] = "Permutations";
Permutations.main(null);