title: Tomcat as Clients communicating with multiple separated Servers via SSL
body: <p>Here is the scenario:</p>
  
  <ul>
  <li>I have multiple application servers running locally for now (should be running in different host) --> each is listening on different port (at localhost).</li>
  <li>I have a single client application running on Tomcat.</li>
  <li>When startup Tomcat, login with different user's details with connect to different (above) servers remotely.</li>
  </ul>
  
  <p>My problem is:</p>
  
  <ul>
  <li>First, I startup Tomcat and logged in as userA, it then connected successfully to serverA(localhost:1000).</li>
  <li>Then I logged out.</li>
  <li>Logged in again as userB, it did NOT connect to serverB(localhost:1001) as expected; instead, it gave exception 
  "javax.net.ssl.SSLHandshakeException: Received fatal alert: certificate_unknown"</li>
  <li>However, if I restart Tomcat, and login as userB first, it then connects successfully to serverB.</li>
  </ul>
  
  <p>Does anyone know what the problem is?
  I really appreciate any suggestion :)</p>
  
  <p>Code for client Tomcat:</p>
  
  <pre><code>SetupClientKeystore();
  SetupServerKeystore();
  SSLContext context = SetupSSLContext();
  SSLSocketFactory socketFactory = sslContext.getSocketFactory();
  SSLSocket socket = (SSLSocket) socketFactory.createSocket(hostname, portNo);
  GZIPOutputStream gZipOut = new GZIPOutputStream(socket.getOutputStream()); // no trust certificate found throws here
  </code></pre>
  
  <p>Code for serverA and B:</p>
  
  <pre><code>setupClientKeyStore();
  setupServerKeystore();
  setupSSLContext();
  server = new ServerSocket(portNo);
  SSLServerSocketFactory socketFactory = sslContext.getServerSocketFactory();
  serverSocket = (SSLServerSocket) socketFactory.createServerSocket(portNo);
  serverSocket.setNeedClientAuth(true);
  while ( true )   
  {
      Socket client = serverSocket.accept();
  inStream = client.getInputStream();
      BufferedInputStream bufferedIn = new BufferedInputStream(inStream); //unknown_certificate throws here
      //do something here.....
  }
  </code></pre>