Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JavaHttpServer。HttpHandler调用了两次_Java_Http_Web - Fatal编程技术网

JavaHttpServer。HttpHandler调用了两次

JavaHttpServer。HttpHandler调用了两次,java,http,web,Java,Http,Web,我需要沟通我的java应用程序和我的网站。出于某种原因,我选择使用可用的HttpServer类。(我不太懂PHP)。我看了这个问题: 这是我使用的HttpHandler代码: public class NexusHttpHandler implements HttpHandler{ private String response; public NexusHttpHandler(String response){ this.response=response;

我需要沟通我的java应用程序和我的网站。出于某种原因,我选择使用可用的HttpServer类。(我不太懂PHP)。我看了这个问题:

这是我使用的HttpHandler代码:

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();
    }

}
出于某种原因,每次页面刷新都会调用两次“我被调用”。这是完整的输出:

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], [*/*]]

有人能告诉我为什么叫两次吗?看起来它和缓存有关,我必须阅读一些关于Http协议的内容。我应该如何识别每个请求类型?我应该如何处理它们?

尝试输出浏览器请求的路径(该路径是域后URL的一部分)

请记住,输出中包含的所有资源(css、外部javascript文件、图片、flash内容)(假设此输出为HTML)都是使用单独的附加HTTP请求从服务器加载的


大多数浏览器也会从网站获取额外的信息,例如
favicon.ico
文件,我怀疑这里可能就是这种情况。

尝试输出浏览器请求的路径(该路径是域后URL的一部分)

请记住,输出中包含的所有资源(css、外部javascript文件、图片、flash内容)(假设此输出为HTML)都是使用单独的附加HTTP请求从服务器加载的


大多数浏览器也会从网站上获取额外的信息,例如
favicon.ico
文件,我怀疑这里可能就是这种情况。

尝试在控制台上打印处理程序对象,以确保是否连接了多个处理程序,或者是同一个处理程序被调用了两次,还要记录线程,以便我们知道哪个线程实际上正在调用处理程序

大概是这样的:

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();
    }

}

尝试在控制台上打印处理程序对象,以确保是否连接了多个处理程序,或者是同一个处理程序被调用了两次,还要记录线程,以便了解哪个线程实际调用该处理程序

大概是这样的:

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();
    }

}

浏览器发送2个请求,如果HTML/JSP代码中的某个地方有AJAX调用,则可能会多次触发处理程序(4)


如果难以使用java SOP进行调试,请尝试在html代码中添加console.log语句,并使用Firefox和Firebug插件来观察浏览器在每次单击时发送的post请求。这将为您提供有关何时生成请求以及请求通过POST的详细信息(这可能不明显,因为url中不存在)。看看这个,你就会知道你的处理器什么时候被触发

浏览器发送2个请求,如果HTML/JSP代码中的某个地方有AJAX调用,则可能会多次触发处理程序(4)


如果难以使用java SOP进行调试,请尝试在html代码中添加console.log语句,并使用Firefox和Firebug插件来观察浏览器在每次单击时发送的post请求。这将为您提供有关何时生成请求以及请求通过POST的详细信息(这可能不明显,因为url中不存在)。看看这个,你就会知道你的处理器什么时候被触发

来吧,伙计,浏览器总是第一次发送两个请求,一个是获取实际的url数据,另一个是获取/favicon.ico资源,这是一个小图标,正如你看到的堆栈溢出的膨胀,我从来不知道。我从来没有在allC'mon buddy上研究过web编程,浏览器总是第一次发送两个请求,一个是获取实际的url数据,另一个是获取/favicon.ico资源,这是一个小图标,正如你看到的堆栈溢出膨胀,我从来不知道这一点。我从来没有研究过网络编程