0

I'm developing a HTTP server using HttpServer class. The code is like the following.

  public static void main(String[] args) throws Exception {
    HttpServer server = HttpServer.create(new InetSocketAddress(8989), 0);
    server.createContext("/", new MyHandler());
    server.setExecutor(null); // creates a default executor

    server.start();
  }

  static class MyHandler implements HttpHandler {
    public void handle(HttpExchange httpExchange) throws IOException {
         /*
             Some code here
         */
    }
  }

What I want is to find something (an id variable or an object) that identifyies the actual connection in the handler function.

If I make a break-point in the handler, I debug the server, then I run a client, I can see the content of httpExchange:

enter image description here

I think that connection attribute is a good choice. But I can't find how to get it from httpExchange, or its id.

Is there any suggestion?

Kallel Omar
  • 1,208
  • 2
  • 17
  • 51
  • Don't try to hurry anyone here. Have you tried [`HttpExchange#getAttribute(String)`](https://docs.oracle.com/javase/8/docs/jre/api/net/httpserver/spec/com/sun/net/httpserver/HttpExchange.html#getAttribute-java.lang.String-)? – cassiomolin Jul 29 '16 at 09:18
  • yes I tried with this: httpExchange.getAttribute("connection") it gives null. – Kallel Omar Jul 29 '16 at 09:23
  • Where are you setting this attribute? – cassiomolin Jul 29 '16 at 09:24
  • 1
    Why do you think you need this? Usually your are interested in the session, not the connection. – user207421 Jul 29 '16 at 09:27
  • @EJP yes but I didn't find how to get the session, but when debugging I found that httpExchange contains the httpconnection so I said may be I can exploit it to achieve what I want – Kallel Omar Jul 29 '16 at 09:37
  • @CássioMazzochiMolin I didn't set it. it's just an attempt. the attribute that I'm looking for is set automatically – Kallel Omar Jul 29 '16 at 09:40

0 Answers0