如何从jsp调用java程序的main方法?

时间:2009-07-01 06:43:15

标签: java jsp

我有一个JSP,我从中获取用户的输入。现在,我想将从这个JSP获得的输入作为参数传递给我的java程序的main方法,并从这个JSP运行程序。请帮忙!

提前致谢! Ravilla

3 个答案:

答案 0 :(得分:3)

糟糕的主意。你想要实现什么目标?即使你这样做,通过调用main()作为任何其他任意方法,或通过调用Runtime或其他东西,你将实现的是在服务器上运行的程序。客户会怎么做。

JSP / Servlet / Java Web编程是基于请求/响应的东西。

如果您想使用该程序计算某些内容并计划将输出用作响应。那么为什么不将那个特定的部分作为业务组件或其他东西包括在内并以通常的方式调用它。可能在一个单独的线程中,如果它是一个很长的进程,让用户与应用程序交互并在完成后通知用户。

答案 1 :(得分:1)

一年前我不得不编写类似这样的程序,最后我把它变成了web服务。这允许我从JSP调用我的应用程序。我想你应该试试这个。

答案 2 :(得分:0)

您需要在操作系统级别执行命令。

    Process p = Runtime.getRuntime().exec("java -classpath "..." SomeClassContainingMain ...other arguments);       

        //you need to consume the outputs of the command if output/error is large otherwise the process is going to hang if output/error buffer is full. and create a seperate thead for it (not created here).
        log.debug("PROCESS outputstream : " + p.getInputStream() );
        log.debug("PROCESS errorstream : " + p.getErrorStream());           
    p.waitFor(); // Wait till the process is finished