I want to make a Java-server where I can simply open my web browser and see “Hello World”. So far, I believe that I have set up a server but I don’t know why nothing is displaying in the browser.
I used the following command lines to create the files associated with the certificate.
keytool -genkey -keyalg RSA -keysize 2048 -validity 360 -alias mykey -keystore myKeyStore.jks
keytool -export -alias mykey -keystore myKeyStore.jks -file mykey.cert
keytool -import -file mykey.cert -alias mykey -keystore myTrustStore.jts
Here is my file: Server.java
import java.io.*;
import javax.net.ssl.*;
public class Server{
public static void main(String[] args) throws IOException{
int port = 8080;
System.setProperty("javax.net.ssl.keyStore", "myKeyStore.jks");
System.setProperty("javax.net.ssl.keyStorePassword", "123456");
SSLServerSocketFactory ssf = (SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
SSLServerSocket ss = (SSLServerSocket) ssf.createServerSocket(port);
while(true){
SSLSocket sslsocket = (SSLSocket) ss.accept();
PrintStream out = new PrintStream(sslsocket.getOutputStream(), true);
String msg = "Hello World";
out.println("HTTPS/1.0 200 OK");
out.println("Content-Type: text/html");
out.println(); // Slut på headers
out.print(msg);
out.close();
}
}
}
How can I make it display “Hello World” in the browser? I have tried Firefox and Chrome.