I need to communicate my java app and my web site. For some reason I chose to use HttpServer class available. (I dont really know PHP). I looked at this question: simple HTTP server in Java using only Java SE API
This is the HttpHandler code I used:
public class NexusHttpHandler implements HttpHandler{
private String response;
public NexusHttpHandler(String response){
this.response=response;
}
@Override
public void handle(HttpExchange he) throws IOException {
System.out.println("I am called!");
System.out.println(he.getRequestHeaders().keySet());
System.out.println(he.getRequestHeaders().values());
he.sendResponseHeaders(200, response.length());
OutputStream os = he.getResponseBody();
os.write(response.getBytes());
os.close();
}
}
For some reason "I am called" is called twice for every page refresh. This is the full output:
I am called!
[Cache-control, Host, Accept-encoding, Connection, Accept-language, User-agent, Accept]
[[max-age=0], [localhost:8080], [gzip,deflate,sdch], [keep-alive], [ru-RU,ru;q=0.8,en-US;q=0.6,en;q=0.4], [Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.72 Safari/537.36], [text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8]]
I am called!
[Host, Accept-encoding, Connection, Accept-language, User-agent, Accept]
[[localhost:8080], [gzip,deflate,sdch], [keep-alive], [ru-RU,ru;q=0.8,en-US;q=0.6,en;q=0.4], [Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.72 Safari/537.36], [*/*]]
Could someone tell me why is it called twice? It looks like it is related to cache and I have to read something about Http protocol. How should I identify each of the request type? How should I deal with them?