0

我写了这个 Servlet:

 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws 
    ServletException, IOException {
    response.setContentType("text/event-stream;charset=UTF-8");
    response.setHeader("Cache-Control","no-cache");

    PrintWriter printWriter=response.getWriter();
    printWriter.write("Hello!");
}

还有 index.jsp 中的这个 java 脚本:

<script>
    var resource = new EventSource("/servlet");
    resource.onmessage = function (e) {
        document.getElementById("container").innerHTML = e.data;
    }
</script>

为了在 html5 中创建关于服务器发送事件的演示。我在firefox中检查了jsp页面,我得到了这个错误:the resource from this url is not text什么都没有出现。顺便说一下请求状态是200,OK。那有什么问题?

4

1 回答 1

2

尝试更改响应标头

response.setContentType("text/event-stream");  
于 2013-10-02T12:03:27.557 回答