我想实现一个java服务器和一个php客户端。每次点击网站(php客户端)时,都应该在服务器上执行一个操作。
事实上,我的服务器看起来像:
public static void main(String args[]) {
System.out.println("Signal Server is running.");
try {
socket = new ServerSocket(port);
while (true) {
connection = socket.accept();
InputStreamReader inputStream = new InputStreamReader(connection.getInputStream());
DataOutputStream response = new DataOutputStream(connection.getOutputStream());
BufferedReader input = new BufferedReader(inputStream);
command = input.readLine();
response.writeBytes(responseStr);
response.flush();
System.out.println("Running");
}
} catch (IOException e) {
System.out.println("Fail!: " + e.toString());
}
System.out.println("Closing...");
}
我的HTML网站看起来像:
<?php
if (isset($_POST["Btn1"])){
$address = "localhost";
$port = 4343;
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
$message = 'blablabla';
socket_connect($socket, $address, $port);
socket_sendto($socket, $message, strlen($message), 0, $address, $port);
};
?>
<html>
<head>
<title>Test</title>
</head>
<body>
<form id="form1" method="POST">
<button type="submit" form="form1" id="Btn1" name="Btn1" value="Btn1" title="Btn1">
Btn1</button>
</form>
</body>
</html>
我的问题是,将操作委派给服务器的最佳方式是什么。举个小例子,我在java服务器上有一个方法,它将"Hello"发布到控制台。现在我点击我网站上的一个按钮,这个方法就应该执行了。最好的方法是什么?
我可以用我的方法吗?还是有更好的方法?
PHP运行在服务器端,因此您无法在客户端的网页上执行PHP代码。。。