title: How to manage HTTP request with Java HTTPS server?
body: <p>I have set my own https server with  <code>com.sun.net.httpserver.HttpsServer</code> library. </p>
  
  <p>The code looks like this:</p>
  
  <pre><code>int PORT = 12345;
  File KEYSTORE = new File("path/to/my.keystore");
  String PASS = "mykeystorepass";
  
  HttpsServer server = HttpsServer.create(new InetSocketAddress(PORT), 0);
  
  SSLContext sslContext = SSLContext.getInstance("TLS");
  char[] password = PASS.toCharArray();
  KeyStore ks = KeyStore.getInstance("JKS");
  FileInputStream fis = new FileInputStream(KEYSTORE);
  ks.load(fis, password);
  KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
  kmf.init(ks, password);
  
  TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
  tmf.init(ks);
  
  sslContext.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
  
  server.setHttpsConfigurator(new HttpsConfigurator(sslContext) {
      public void configure(HttpsParameters params) {
          try {
              SSLContext c = SSLContext.getDefault();
              SSLEngine engine = c.createSSLEngine();
              params.setNeedClientAuth(false);
              params.setCipherSuites(engine.getEnabledCipherSuites());
              params.setProtocols(engine.getEnabledProtocols());
              SSLParameters defaultSSLParameters = c.getDefaultSSLParameters();
              params.setSSLParameters(defaultSSLParameters);
          } catch(Exception ex) {
              ex.printStackTrace();
          }
      }
  });
  
  server.createContext("/", new Handler());
  server.setExecutor(null);
  server.start();
  </code></pre>
  
  <p>Everything is working, but now I am trying to manage HTTP requests coming to this HTTPS server on the same port. When I open the webpage (which is processed by the my HttpHandler) with HTTP protocol, it says error <code>ERR_EMPTY_RESPONSE</code>, only HTTPS protocol works.</p>
  
  <p>I would like to redirect all incoming connections from HTTP to HTTPS automatically. Or it would be great to use different HttpHandlers for both protocols.</p>
  
  <p>How can I achieve this?</p>