I am learning to create the web socket server in java with tomcat,
but when I run the tomcat9.0 with the sample code,
I always get the below error in the chrome developer tool response.
**web socket failed: Error during WebSocket handshake: Unexpected response code: 404** I query the much the reference, I still not resolve the problem,
I am use the tomcat9.0 & jre 1.8.0, and I refer the blog.
I create the server code(project -> new -> Servlet file) is
package idv.yu; import java.io.IOException; import javax.websocket.OnClose; import javax.websocket.OnError; import javax.websocket.OnMessage; import javax.websocket.OnOpen; import javax.websocket.Session; import javax.websocket.server.ServerEndpoint; @ServerEndpoint(value = "/endpoint") public class MyWebsocket { @OnOpen public void onOpen(Session session) { System.out.println("onOpen::" + session.getId()); } @OnClose public void onClose(Session session) { System.out.println("onClose::" + session.getId()); } @OnMessage public void onMessage(String message, Session session) { System.out.println("onMessage::From=" + session.getId() + " Message=" + message); try { session.getBasicRemote().sendText("Hello Client " + session.getId() + "!"); } catch (IOException e) { e.printStackTrace(); } } @OnError public void onError(Throwable t) { System.out.println("onError::" + t.getMessage()); } } My project tree structure is below:
In my chrome developer tool I set the code
class WebSocketClient { constructor(protocol, hostname, port, endpoint) { this.webSocket = null; this.protocol = protocol; this.hostname = hostname; this.port = port; this.endpoint = endpoint; } getServerUrl() { return this.protocol + "://" + this.hostname + ":" + this.port + this.endpoint; } connect() { try { this.webSocket = new WebSocket(this.getServerUrl()); // // Implement WebSocket event handlers! // this.webSocket.onopen = function(event) { console.log('onopen::' + JSON.stringify(event, null, 4)); } this.webSocket.onmessage = function(event) { var msg = event.data; console.log('onmessage::' + JSON.stringify(msg, null, 4)); } this.webSocket.onclose = function(event) { console.log('onclose::' + JSON.stringify(event, null, 4)); } this.webSocket.onerror = function(event) { console.log('onerror::' + JSON.stringify(event, null, 4)); } } catch (exception) { console.error(exception); } } getStatus() { return this.webSocket.readyState; } send(message) { if (this.webSocket.readyState == WebSocket.OPEN) { this.webSocket.send(message); } else { console.error('webSocket is not open. readyState=' + this.webSocket.readyState); } } disconnect() { if (this.webSocket.readyState == WebSocket.OPEN) { this.webSocket.close(); } else { console.error('webSocket is not open. readyState=' + this.webSocket.readyState); } } } the last I set the command below:
var client = new WebSocketClient('ws', '127.0.0.1', 8080, '/WebsocketDemo/endpoint'); client.connect(); But I get the error:
Have anyone known how to resolve the problem?
should the "web.xml" had to set any something?
my web.xml is default value, and I had check the project have websocket-api.jar file in the library. 
Thank you very much.
-- edit--
my WebContent/Web-INF/web.xml content below:
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <display-name>WebsocketDemo</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> </web-app> 

http://localhost:XXXX/WebsocketDemo/?