简体   繁体   English

Java Socket HTML响应

[英]Java Socket HTML response

I'm trying to get a Java socket to send a simple HTML response to a browser. 我正在尝试使用Java套接字向浏览器发送简单的HTML响应。

Here's my Java code: 这是我的Java代码:

    Socket socket = server.accept();
    BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
    String s;
    // this is a test code which just reads in everything the requester sends
    while ((s = in.readLine()) != null)
    {
        System.out.println(s);
        if (s.isEmpty())
        {
            break;
        }
    }
    // send the response to close the tab/window
    String response = "<script type=\"text/javascript\">window.close();</script>";

    PrintWriter out = new PrintWriter(socket.getOutputStream());
    out.println("HTTP/1.1 200 OK");
    out.println("Content-Type: text/html");
    out.println("Content-Length: " + response.length());
    out.println();
    out.println(response);
    out.flush();
    out.close();
    socket.close();

server is a ServerSocket set to automatically pick an open port to use. server是一个ServerSocket,设置为自动选择要使用的开放端口。

The idea is any webpage redirected to http:\\\\localhost:port (where port is the port server is listening to) is closed automatically. 这个想法是任何重定向到http:\\\\localhost:port网页( portserver正在侦听的端口)自动关闭。

When this code gets run, my browser receives the response and I've verified that it receives all the information that I'm sending. 当此代码运行时,我的浏览器会收到响应,并且我已经验证它收到了我正在发送的所有信息。

However, the window/tab isn't closing, and I can't even close the tab by manually issuing a window.close(); 但是,窗口/选项卡没有关闭,我甚至无法通过手动发出window.close(); command into the Javascript console for my browser. 命令进入我的浏览器的Javascript控制台。

What am I missing here? 我在这里错过了什么? I know that an html page with the given content should automatically close the window/tab, so why isn't this working? 我知道具有给定内容的html页面应该会自动关闭窗口/选项卡,那么为什么这不起作用呢? I'm testing this on Google Chrome. 我正在Google Chrome上对此进行测试。

I've tried a more complete html webpage, but still no luck. 我已经尝试了一个更完整的html网页,但仍然没有运气。

Here's what the browser is reporting as the page source: 以下是浏览器作为页面源报告的内容:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<script type="text/javascript">window.close();</script>
</head>
<body>
</body>
</html>

To summarize the comments: 总结评论:

The root issue is actually the one found here , where window.close() won't close the current window/tab. root问题实际上是在这里找到的问题 ,其中window.close()不会关闭当前窗口/选项卡。

I looked up the MDN Documentation and found this: 我查看了MDN文档 ,发现了这个:

When this method is called, the referenced window is closed. 调用此方法时,将关闭引用的窗口。

This method is only allowed to be called for windows that were opened by a script using the window.open method. 仅允许对使用window.open方法由脚本打开的窗口调用此方法。 If the window was not opened by a script, the following error appears in the JavaScript Console: Scripts may not close windows that were not opened by script. 如果脚本未打开窗口,则JavaScript控制台中将显示以下错误:脚本可能无法关闭脚本未打开的窗口。

Apparently Google Chrome wasn't considering the current window to be opened by the script. 显然谷歌Chrome没有考虑脚本打开当前窗口。 I also tried this in Firefox which exhibits the same behavior. 我也在Firefox中尝试过这种行为。

To get around this, I must first open the current window using a script. 要解决这个问题,我必须首先使用脚本打开当前窗口。

<script type="text/javascript">
    window.open('', '_self', '');
    window.close();
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM