简体   繁体   English

在Java程序运行时从PHP向Java程序提供输入

[英]Giving input to a Java program from PHP while Java program is running

I am getting user input from a website, though PHP, the Java program is running on my server, it takes the user input, does some magic and spits back a response in the form of an HTML page. 我正在从一个网站上获得用户输入,尽管PHP是Java程序正在服务器上运行,但它接受用户输入,进行一些魔术操作并以HTML页面的形式返回响应。

The problem that I am having is that I only want to start the java program once, because there is some heavy lifting that needs to be done only once. 我遇到的问题是我只想启动Java程序一次,因为繁琐的工作只需要执行一次。 But then I want to keep the Java running and just get it to react to the input from the website. 但是,然后我想保持Java的运行状态,只是让它对网站的输入做出反应。

My method to pass information from PHP is by calling a command prompt command, with the appropriate args. 我从PHP传递信息的方法是使用适当的args调用命令提示符命令。 Now obviously I can't use args after the program started. 现在显然在程序启动后我无法使用args。

I need some sort of Buffer or Input Stream, something that will give me pretty much instantaneous response time. 我需要某种类型的缓冲区或输入流,这将使我获得几乎瞬时的响应时间。

Thank you in advance 先感谢您

I can think of three alternatives: 我可以想到三种选择:

  1. Using sockets. 使用套接字。 You can make your java application become some sort of a "server" that receives request and then "respond" to them. 您可以使Java应用程序成为某种“服务器”,该服务器接收请求,然后对其进行“响应”。 The same way your HTTP server works (but of course on a different port). HTTP服务器的工作方式相同(但当然在不同的端口上)。 This article explains how to work with java sockets . 本文介绍了如何使用Java套接字 ( searched for:java socket programming ) And this one explains working with sockets from php ( searched for:php socket programming ) 搜索:java套接字编程 )这一步解释了如何使用 php中的套接字搜索:php套接字编程

  2. Making your java application single-instance check this to save start-up time, but this way you will have to write response in files or something. 使Java应用程序为单实例进行检查可以节省启动时间,但是通过这种方式,您将不得不在文件或其他内容中写入响应。

  3. Put your java code in a servlet, run it inside a web container, and redirect the requests from your php page to the servlet. 将您的Java代码放在servlet中,在Web容器中运行它,然后将请求从您的php页面重定向到servlet。 refer to ths question 提到这个问题

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

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