2

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?

Community
  • 1
  • 1
black
  • 357
  • 3
  • 18
  • C'mon buddy, browsers always send two request for the first time, one is getting the actual url data, and another one is for getting /favicon.ico resource, it's small icon as you are seeing of stack overflow's –  Jul 14 '13 at 21:46
  • Well, I never knew that. I never looked into web programming at all – black Jul 15 '13 at 11:46

3 Answers3

5

Try outputting the path that the browser requested (which is the part of the URL after the domain).

Remember that all resources (css, external javascript files, pictures, flash stuff) that are included in your output (assuming this output is HTML) are loaded from the server using seperate, additional HTTP requests.

Most browsers also fetch additional infos from a website, such as a favicon.ico file, i suspect that this might be the case here.

f1sh
  • 11,489
  • 3
  • 25
  • 51
0

Try to print the handler object on the console, to make sure if there are more than one handlers attached or is it the same handler being called twice, also log the thread so we get to know which thread is actually calling the handler.

Something like this:

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("handler object = " + this);
        System.out.println("called by thread = " + Thread.currentThread());
        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();
    }

}
0

Browsers sends 2 requests, If there is an AJAX call some where in the HTML/JSP code then it might trigger the handler multiple times(4) .

http://www.w3schools.com/ajax/ajax_xmlhttprequest_onreadystatechange.asp

If it is difficult to debug using java SOP , try adding console.log statements in the html code and use Firefox with Firebug add-on to observe the post request being sent by the browser on each click . This will give you the details on which request gets generated when and the Request getting passed POST(this might not be obvious as it is not present in the url). Looking at this you will know when your handler gets triggered .

trooper31
  • 182
  • 1
  • 2
  • 9