Is it possible to implement a web server so that the pages are processed using the PHP language, and ServerSocket was launched in Java? That is something like the following:

 package main; import java.io.BufferedReader; import java.io.FileInputStream; import java.io.InputStreamReader; import java.net.ServerSocket; import java.net.Socket; class Main { static ServerSocket i1; public static void main(String[] i2) { try { i1 = new ServerSocket(80); new Thread(new Runnable() { @Override public void run() { while (true) { try { Socket i2 = i1.accept(); // Ответить клиенту веб-сервера нужно главной страницой // Для этого откроем этот файл "index.php" BufferedReader i3 = new BufferedReader(new InputStreamReader(new FileInputStream("index.php"))); String i4 = ""; while (true) { String i5 = i3.readLine(); if (i5 == null) { i3.close(); break; } else { i4 += i5 + System.lineSeparator(); } } /* Пусть содержимое файла "index.php" будет следующим: <?php echo 'Hello world!'; ?> Его нужно обработать в PHP обработчике примерно так: i4 = i4.обработать_в_PHP(); И, затем, если сделать вывод "System.out.println(i4)", должно быть выведено следующее: Hello world! И, в итоге клиент должен получить строчку "Hello world!": */ i2.getOutputStream().write(i4.getBytes()); i2.getOutputStream().close(); } catch (Exception e1) { e1.printStackTrace(); } } } }).start(); } catch (Exception e1) { e1.printStackTrace(); } } } 
  • Theoretically yes, but just writing in Java is more rational. - D-side

3 answers 3

In java, there is a Java Scripting API (JSR 223) that allows you to execute scripts 'inside java'. You can make your own implementation for PHP or use, for example PHP / Java Bridge .

In addition, there is Quercus - the implementation of the PHP engine in java for the Resin server

    Surely from javovskogo server, you can proxy requests for PHP-FPM via the CGI protocol. But if you need to render only one php file (well, or you have an entry point into the page through one file), then you can run the system command from java:

     php index.php 

    stdout and stderr of this command will be your "page rendered in php". The problem here will be that php will not know anything about the request parameters (cookies, headers, etc.)

    http://coreygoldberg.blogspot.ru/2008/06/java-run-system-command-and-return.html

    • So the session will also be impossible to implement? Very sorry. They are needed. - nick
    • It is possible but manually and with squats in java. If you correctly proxy on FPM, then there the sessions will be held automatically. - FreeDooM
    • You can also proxy the request to port 81 from java itself, and hang up php with the built-in web server to port 81: php -S localhost: 81 index.php
    • I do not understand. You can explain in more detail how to implement the session. - nick
    • Parsit HTTP protocol in javascript, rip out the headers and slip them into index.php, again, manually, for example, through the parameters: php index.php header11 = ... header2 = ... $ argv. - FreeDooM

    Run php-fpm, and then use the FastCGI protocol to transfer the request parameters and the script for execution there.

    Something like: https://github.com/Happyr/fcgi4j