title: java-server websocket failed: Error during WebSocket handshake: Unexpected response code: 404
body: <p>I am learning to create the web socket server in java with tomcat,</p>
  
  <p>but when I run the tomcat9.0 with the sample code, </p>
  
  <p>I always get the below error in the chrome developer tool response.</p>
  
  <pre><code> **web socket failed: Error during WebSocket handshake: Unexpected response code: 404**
  </code></pre>
  
  <p>I query the much the reference, I still not resolve the problem,</p>
  
  <p>I am use the tomcat9.0 &amp; jre 1.8.0, and I refer the <a href="https://www.pegaxchange.com/2018/01/28/websocket-server-java/" rel="nofollow noreferrer">blog</a>.</p>
  
  <p>I create the server code(project -> new -> Servlet file)  is </p>
  
  <pre><code> 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());
      }
   }
  </code></pre>
  
  <p>My project tree structure is below:</p>
  
  <p><a href="https://i.stack.imgur.com/Y8BLA.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Y8BLA.png" alt="enter image description here"></a></p>
  
  <p>In my chrome developer tool I set the code</p>
  
  <pre><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);
           }
       }
   }
  </code></pre>
  
  <p>the last I set the command below:</p>
  
  <pre><code> var client = new WebSocketClient('ws', '127.0.0.1', 8080, '/WebsocketDemo/endpoint');
   client.connect();
  </code></pre>
  
  <p>But I get the error:</p>
  
  <p><a href="https://i.stack.imgur.com/Ay3iD.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Ay3iD.png" alt="enter image description here"></a></p>
  
  <p>Have anyone known how to resolve the problem?</p>
  
  <p>should the "web.xml" had to set any something?</p>
  
  <p>my web.xml is default value, and I had check the project have websocket-api.jar file in the library.
  <a href="https://i.stack.imgur.com/Mj7Ld.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Mj7Ld.png" alt="enter image description here"></a></p>
  
  <p>Thank you very much.</p>
  
  <p>-- edit--</p>
  
  <p>my WebContent/Web-INF/web.xml content below:</p>
  
  <pre><code> &lt;?xml version="1.0" encoding="UTF-8"?&gt;
   &lt;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"&gt;
     &lt;display-name&gt;WebsocketDemo&lt;/display-name&gt;
     &lt;welcome-file-list&gt;
       &lt;welcome-file&gt;index.html&lt;/welcome-file&gt;
       &lt;welcome-file&gt;index.htm&lt;/welcome-file&gt;
       &lt;welcome-file&gt;index.jsp&lt;/welcome-file&gt;
       &lt;welcome-file&gt;default.html&lt;/welcome-file&gt;
       &lt;welcome-file&gt;default.htm&lt;/welcome-file&gt;
       &lt;welcome-file&gt;default.jsp&lt;/welcome-file&gt;
     &lt;/welcome-file-list&gt;
   &lt;/web-app&gt;
  </code></pre>