title: HTTPS in Undertow
body: <p>I have a web service that runs on Tomcat and decided to try using Undertow instead. Generally, it's quite good, but I need HTTPS support and cannot enable it. Here is some helloworld code:</p>
  
  <pre><code>DeploymentInfo servletBuilder = deployment()
              .setClassLoader(ServletServer.class.getClassLoader())
              .setContextPath("/")
              .setDeploymentName("AuthenticationService.war")
              .addServlet(servlet("WSServlet", WSServlet.class))
              .addListener(listener(WSServletContextListener.class))
              .setResourceManager(new FileResourceManager(new File("src/main/webapp"), 100));
  
      DeploymentManager manager = defaultContainer().addDeployment(servletBuilder);
      manager.deploy();
  
      HttpHandler servletHandler = manager.start();
  
      SSLContext context = createSSLContext(loadKeyStore("server-keystore.jsk"), loadKeyStore("server-truststore.jks"));
      PathHandler path = Handlers.path(servletHandler);
  
      Undertow server = Undertow.builder()
              .addHttpsListener(8443, "localhost", context)
              .setHandler(path)
              .build();
      server.start();
  </code></pre>
  
  <p>And the createSSLContext method:</p>
  
  <pre><code>private static SSLContext createSSLContext(final KeyStore keyStore, final KeyStore trustStore) throws Exception {
      KeyManager[] keyManagers;
      KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
      keyManagerFactory.init(keyStore, /* password */);
      keyManagers = keyManagerFactory.getKeyManagers();
  
      TrustManager[] trustManagers;
      TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
      trustManagerFactory.init(trustStore);
      trustManagers = trustManagerFactory.getTrustManagers();
  
      SSLContext sslContext;
      sslContext = SSLContext.getInstance("TLS");
      sslContext.init(keyManagers, trustManagers, null);
  
      return sslContext;
  }
  </code></pre>
  
  <p>And loadKeyStore method:</p>
  
  <pre><code>private static KeyStore loadKeyStore(String name) throws Exception {
      final InputStream stream = new FileInputStream(name);
  
      try(InputStream is = stream) {
          KeyStore loadedKeystore = KeyStore.getInstance("JKS");
          loadedKeystore.load(is, /* password */);
          return loadedKeystore;
      }
  }
  </code></pre>
  
  <p>The server is starting, but trying to send requests to <a href="https://localhost:8443/" rel="nofollow">https://localhost:8443/</a>... has no effect, no logs or exceptions or some reaction. When using <a href="http://localhost:8443" rel="nofollow">http://localhost:8443</a> it throws exception </p>
  
  <pre><code>javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?
  </code></pre>
  
  <p>I am new to all web technologies, so all that may be strange. So what's wrong here? </p>