The servlet of java is used to implement the server side of EventSource, and the client is disconnected after each request is received. Why?

problem description

the servlet of java is used to implement the server side of EventSource, and the client will disconnect after receiving each request

the environmental background of the problems and what methods you have tried

jdk version is 1.7
servlet version is servlet-api:2.5
when nodejs is used as the backend, it will not be disconnected every time. After a connection, it can receive data from the server many times.

related codes

server code:

public class TestEventSource extends HttpServlet{

    private static final long serialVersionUID = 1L;
    
    public void doGet(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
        response.setContentType("text/event-stream");
        response.setHeader("Cache-Control","no-cache");
        response.setHeader("Connection","keep-alive");
        response.setHeader("Access-Control-Allow-Origin","*");
        response.setCharacterEncoding("utf-8");
        PrintWriter out = response.getWriter();
        out.print("data: testEventSource\n");
        out.flush();
    }
}

client code:

const evtSource = new EventSource("/y/adm/market/message");
evtSource.onopen = function onopen() {
  console.log("");
};
evtSource.onmessage = function onmessage(e) {
  console.log(e.data);
};
evtSource.onerror = function onerror(e) {
  console.log("onerror");
};

what result do you expect? What is the error message actually seen?

after the client initiates the request, it can receive the data from the server, but immediately disconnects, triggering the onerror method, and then loops
clipboard.png

every 3 seconds.

as far as I understand it, EventSource should be a request, keep the connection, continue to receive the data returned by the server, should not be disconnected. In fact, it is true to use nodejs as the server, which can receive data from the server multiple times with one request;

what is the problem? Ask the boss for advice

Aug.23,2021

does the landlord know why now? I don't quite understand either. Can you say


has the landlord solved it? Solve

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b306d0-2bd1d.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b306d0-2bd1d.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?