简体   繁体   English

如何从jsp调用Java程序的主要方法?

[英]How to invoke main method of java program from a jsp?

I have a JSP from which I'm getting the input from the users. 我有一个JSP,可以从用户那里得到输入。 Now, I'd like to pass the input that get from this JSP as arguments to the main method of my java program and run the program from this JSP. 现在,我想将从此JSP获得的输入作为参数传递给我的Java程序的main方法,并从此JSP运行该程序。 Some help please! 请帮忙!

Thanks in advance! 提前致谢! Ravilla Ravilla

Bad idea. 馊主意。 What are you trying to achieve? 您想达到什么目的? Even if you do that, by calling main() as any other arbitrary method, or by invoking Runtime or something, What you will achieve is a program running on server. 即使这样做,也可以通过将main()作为其他任意方法来调用,或者通过调用Runtime或其他方法来实现,但您将获得的结果是在服务器上运行的程序。 What the client would do with that. 客户会怎么做。

JSP/Servlet/Java Web programming is a request/response based stuff. JSP / Servlet / Java Web编程是基于请求/响应的内容。

If you want to compute something using that program and planning to use the output as a response. 如果要使用该程序计算某些内容并计划将输出用作响应。 Then why not include that particular piece as business component or something and call that in a usual way. 然后,为什么不将该特定部分包括为业务组件或某些东西,并以通常的方式进行调用。 May be in a separate thread, if its a long process, let the user interact with the application and notify user once it is done. 可能在单独的线程中,如果过程很长,请让用户与应用程序进行交互,并在完成后通知用户。

I had to program something like this a year ago and I ended up making it a webservice. 一年前,我不得不编写类似的程序,最终使它成为一个Web服务。 This allowed my app to be called from a JSP. 这样就可以从JSP调用我的应用程序。 I think you should try this. 我认为您应该尝试一下。

you need to execute command at OS level. 您需要在操作系统级别执行命令。

    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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM