0

I am teaching a Java course. Instead of teaching the Java GUI libraries (awt or swing), I want to teach my students to use HTML for GUI, and use Java only in the server side. So, I want to build a Java server and an HTML page that connect to each other using sockets. I want to use only raw Java, so that my students do not have to install third-party libraries. So far, I built the following Java server, which just accepts strings and reverses them:

import java.io.*;
import java.net.*;

public class ReversingServer {
    public static void main(String[] args) throws Exception {
        int port = 8005;
        ServerSocket serverSocket = new ServerSocket(port);
        while (true) {
            final Socket socket = serverSocket.accept();
            BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            PrintWriter writer    = new PrintWriter(socket.getOutputStream(), true);

            String input = reader.readLine();
            System.out.println("input="+input);
            String output = (input==null? 
                    null: 
                    new StringBuilder(input).reverse().toString()); 
            writer.println("The reverse of "+input+" is ... ");
            writer.println("... "+output+"!");
            writer.close();
        }
    }
}

When I run this application, I can do "telnet localhost 8005" and send a string such as "abc" and get the response "the reverse of abc is cba".

I also started building the HTML GUI:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Test the Reversing Server</title>
</head>

<body>
<div>
Enter a string to reverse:
<input name="input"></input>
<input type="submit"></input>
</div>

<div id='response' style='border:solid 1pt black'>
&nbsp;
</div>

</body>
</html>

But here I am stuck - how do I make the HTML page connect to the Java server?

Erel Segal-Halevi
  • 33,955
  • 36
  • 114
  • 183
  • HTML communicates over the HTTProtocol - If it were so easier to write your own web server there would be no need for Tomcat and others – Scary Wombat Oct 13 '17 at 07:32
  • see https://devcenter.heroku.com/articles/create-a-java-web-application-using-embedded-tomcat – Scary Wombat Oct 13 '17 at 07:33
  • @ScaryWombat this tutorial requires knowledge of Maven, which is beyond the current knowledge of my students. Isn't there a simpler solution? – Erel Segal-Halevi Oct 13 '17 at 08:34
  • https://stackoverflow.com/questions/2717294/create-a-simple-http-server-with-java ? –  Oct 13 '17 at 08:38
  • @RC the answers there are over 7 years old, do not handle sockets, and do not contain code for connecting from HTML... – Erel Segal-Halevi Oct 13 '17 at 09:01
  • if the question is how to make a web server in java it still apply. If the question is how to do the "html" part, see https://stackoverflow.com/questions/12407778/connecting-to-tcp-socket-from-browser-using-javascript. My opinion is that we have an X/Y problem here, the usual way to do this is to have an http server and use a standard html form –  Oct 13 '17 at 09:45
  • @RC this looks more related. The answers are from 2013, and they say that it is impossible to connect from HTML to raw TCP sockets. Is this still impossible in 2017? – Erel Segal-Halevi Oct 13 '17 at 09:48
  • I don't know. I added the javascript tag because this is more javascript than java –  Oct 13 '17 at 09:51

0 Answers0