我需要传达我的 java 应用程序和我的网站。出于某种原因,我选择使用可用的 HttpServer 类。(我真的不知道 PHP)。我看了这个问题:simple HTTP server in Java using only Java SE API
这是我使用的 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 协议的内容。我应该如何识别每个请求类型?我应该如何处理它们?