title: The correct way to pass data to/from a java HttpHandler class for java HttpServer class
body: <p>I have a java HttpHandler that I am using to test an application. In the HttpHandler each http request is handled in a separate thread and is passed the HttpExchange. But I need to access data from the main thread and class (the one that setup the HttpServer and HttpHandler) so that HttpHandler can send back the correct response for the current test being run. How is the best way to get this data passed in or accessible by the HttpHandler class? I cannot add another parameter to the HttpHandler#handle since that is defined by the HttpHandler &amp; used by the HttpServer, and I can not access none static methods in the main class. I will also need to push messages from the HttpHandler back to the main class to log.</p>
  
  <p>Thanks</p>
  
  <p>A sample of my code:</p>
  
  <pre><code>class RequestHandler implements HttpHandler {
  
      @Override
      public void handle(HttpExchange exchange)
       {
          // do a bunch of stuff with the request that come in.
      }
  }
  
  
  public class MainClass
  {
      public static void main(String[] args)
      {
          HttpServer server;
          ExecutorService excutor;
          InetSocketAddress addr = new InetSocketAddress(ipAdd, ipPort);
          server = HttpServer.create(addr, 0);
          server.createContext("/", new RequestHandler());
          excutor = Executors.newCachedThreadPool();
          server.setExecutor(excutor);
          server.start();
         // do a bunch of stuff that uses the server
      }
  </code></pre>