title: WebSocket connection to 'ws://localhost:8080/CollabEdit/echo' failed: Error during WebSocket handshake: Unexpected response code: 404
body: <p>I am trying to use websockets in my Servlets project.
  J2EE ver. :3.1
  Server: Glassfish 4.0
  But I get an error that says :</p>
  
  <pre><code>WebSocket connection to 'ws://localhost:8080/CollabEdit/echo' failed: 
  Error during WebSocket handshake: Unexpected response code: 404
  </code></pre>
  
  <p>Also, I get <strong>java.lang.Instantiation</strong> exception when I deploy this on server.</p>
  
  <p>The files that I use as client and server for WebSockets works well when there are only those 2 files in project.
  Since I integrated it in my servlets project, it stopped working.So, I doubt it is somewhere because of the workflow we have:</p>
  
  <p>The work flow is as follows in the application:</p>
  
  <ol>
  <li><p>index.html -->This is where the user logs in</p></li>
  <li><p>LoginServlet is called which calls an html create_file.html</p></li>
  <li><p>An html form that asks user to give the file name to be created and choose an extension for it.
  <strong>When this record is inserted in database, a corresponding GUID is generated.</strong></p></li>
  <li><p>This data goes to a servlet called Collaborate.java .Collaborate.java retrieves this GUID and created a URL "dynamically" using this GUID prepended witH string Document.</p></li>
  <li><p>Once the URL is generated dynamically using this GUID, control is forwarded to a servlet called Document.java(with mapping Document/*) which only forwards the control to main.js.</p></li>
  <li><p>This js file is the client end (has url like <a href="http://applicationname/Document/GUID" rel="nofollow">http://applicationname/Document/GUID</a>), calls web socket server as follows:</p>
  
  <p>var websocket = new websocket("ws://localhost:8080/applicationname/echo");</p></li>
  <li><p>websocket server has this annotation:</p>
  
  <p>@ServerEndpoint("/echo") </p></li>
  </ol>
  
  <p>What exactly looks wrong in this flow that may give this error ?</p>
  
  <p>Edit: When I deploy this application on server, I get an exception that says :</p>
  
  <pre><code> java.lang.InstantiationException
      at org.apache.catalina.core.ApplicationFilterConfig.&lt;init&gt;(ApplicationFilterConfig.java:135)
      at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:5297)
      at org.apache.catalina.core.StandardContext.start(StandardContext.java:5909)
      at com.sun.enterprise.web.WebModule.start(WebModule.java:691)
      at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:1041)
      at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:1024)
      at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:747)
      at com.sun.enterprise.web.WebContainer.loadWebModule(WebContainer.java:2278)
      at com.sun.enterprise.web.WebContainer.loadWebModule(WebContainer.java:1924)
      at com.sun.enterprise.web.WebApplication.start(WebApplication.java:139)
      at org.glassfish.internal.data.EngineRef.start(EngineRef.java:122)
      at org.glassfish.internal.data.ModuleInfo.start(ModuleInfo.java:291)
      at org.glassfish.internal.data.ApplicationInfo.start(ApplicationInfo.java:352)
      at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:497)
      at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:219)
      at org.glassfish.deployment.admin.DeployCommand.execute(DeployCommand.java:491)
      at com.sun.enterprise.v3.admin.CommandRunnerImpl$2$1.run(CommandRunnerImpl.java:527)
      at com.sun.enterprise.v3.admin.CommandRunnerImpl$2$1.run(CommandRunnerImpl.java:523)
      at java.security.AccessController.doPrivileged(Native Method)
      at javax.security.auth.Subject.doAs(Subject.java:356)
      at com.sun.enterprise.v3.admin.CommandRunnerImpl$2.execute(CommandRunnerImpl.java:522)
      at com.sun.enterprise.v3.admin.CommandRunnerImpl.doCommand(CommandRunnerImpl.java:546)
      at com.sun.enterprise.v3.admin.CommandRunnerImpl.doCommand(CommandRunnerImpl.java:1423)
      at com.sun.enterprise.v3.admin.CommandRunnerImpl.access$1500(CommandRunnerImpl.java:108)
      at com.sun.enterprise.v3.admin.CommandRunnerImpl$ExecutionContext.execute(CommandRunnerImpl.java:1762)
      at com.sun.enterprise.v3.admin.CommandRunnerImpl$ExecutionContext.execute(CommandRunnerImpl.java:1674)
      at com.sun.enterprise.v3.admin.AdminAdapter.doCommand(AdminAdapter.java:534)
      at com.sun.enterprise.v3.admin.AdminAdapter.onMissingResource(AdminAdapter.java:224)
      at org.glassfish.grizzly.http.server.StaticHttpHandler.service(StaticHttpHandler.java:297)
      at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:246)
      at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:191)
      at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:168)
      at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:189)
      at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
      at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:288)
      at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:206)
      at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:136)
      at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:114)
      at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
      at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:838)
      at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:113)
      at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:115)
      at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:55)
      at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:135)
      at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:564)
      at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:544)
      at java.lang.Thread.run(Thread.java:744)
  Caused by: javax.servlet.ServletException: Web socket server initialization failed.
      at org.glassfish.tyrus.servlet.TyrusServletFilter.init(TyrusServletFilter.java:135)
      at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:275)
      at org.apache.catalina.core.ApplicationFilterConfig.&lt;init&gt;(ApplicationFilterConfig.java:131)
      ... 46 more
  Caused by: javax.websocket.DeploymentException: Decoder for type: class com.CollabEdit.Message has not been registered.
  Decoder for type: class com.CollabEdit.Message has not been registered.
  Found Equivalent paths. Added path: '/CollabEdit/echo' is equivalent with '/CollabEdit/echo'.
      at org.glassfish.tyrus.core.ErrorCollector.composeComprehensiveException(ErrorCollector.java:83)
      at org.glassfish.tyrus.server.TyrusServerContainer.start(TyrusServerContainer.java:144)
      at org.glassfish.t
  </code></pre>
  
  <p>main.js looks like this:</p>
  
  <pre><code>var webSocket;
  var messages = [];
  var index=-1;
  //var link = "ws" + document.URL.substring(4, document.URL.length) ;
  var link = "ws://localhost:8080/CollabEdit/Document/*";
  var link3 = "ws://localhost:8080/CollabEdit/echo";
  var firstMessage;
  var to=[];
  var path = document.URL;
  var array = path.split("/");
  var temp = array[array.length - 1];
  var link2 = temp.substring(temp.length - 36, temp.length);
  
  require([ 'dojo/on' ], function(on) {
      cm.on('change', function(arg1, arg2) {
          actionToPerform = arg2.origin;
          console.log("typde dekh: ", typeof actionToPerform);
          lineLocation = arg2.from.line;
          charLocation = arg2.from.ch;
          data = arg2.text;
          console.log("Action: ", actionToPerform, " Data: ", data, " line: ",
                  lineLocation, " charLocation: ", charLocation);
          console.log("Location: " + window.location.href);
          var cookie = document.cookie;
          console.log("cookie dekh: ");
          console.log(cookie);
          $.ajax({
              method : "POST",
              url : "../OTServer",
              data : {
  
                  "action" : "'" + actionToPerform + "'",
                  "data" : "'" + data + "'",
                  "lineLocation" : "'" + lineLocation + "'",
                  "charLocation" : "'" + charLocation + "'"
              },
              success: function(data){
                  // yup run kr isse :P
  
                  console.log("in success, got this: ",data);
              },
              error: function(data){
  
             }
          }).done(function(msg) {
              //alert("Response: " + msg);
          });
      });
  });
  
  
  $.ajax({
      method : "POST",
      async : false,
      url : "/CollabEdit/GetInfo",
      success : function(data) {
          console.log("data sent successfully: ",data);
      },
      error : function(data) {
          console.log("Unsuccessful transmission " + data);
      }}).done(
          function(data) {
              firstMessage = {
                  "from":data['email']
              }
      });
  
  
  //this will get the users with whom this file is being shared
  $.ajax({
      method : "POST",
      async : false,
      url : "/CollabEdit/DisplaySEmails",
      data: {"url": link2},
      success : function(data) {
          console.log("data sent successfully: ",data);
      },
      error : function(data) {
          console.log("Unsuccessful transmission " + data);
      }}).done(
          function(data) {
              for(var i in data)
                 to[i] = data[i];
      });
  
  
  //calling the openSocket
  openSocket(link3);
  
  //console.log("chal gya m chajj naal");
  //alert("yaaaaay");
  function openSocket(link3){
      // Ensures only one connection is open at a time
      if(webSocket !== undefined &amp;&amp; webSocket.readyState !== WebSocket.CLOSED){
          //writeResponse("WebSocket is already opened.");
          return;
      }
      // Create a new instance of the websocket
      console.log("LINK ISSSSSSSSSSS" + link);
  
      webSocket = new WebSocket(link3);
  
      /**
       * Binds functions to the listeners for the websocket.
       */
       //calls onOpen of server when connection is established
      webSocket.onopen = function(event){
          if(event.data === undefined)
              return;
          //alert("onOpen executed");
          writeResponse(event.data);
      };
  
    //Calls onMessage ofServer when a message is sent
      webSocket.onmessage = function(event){
          writeResponse(event.data);
      };
  
     ///calls onClose when connection is closed
      webSocket.onclose = function(event){
          messages.innerHTML += "&lt;br/&gt;" + "Connection closed";
      };
  }
  
  /**
   * Sends the value of the text input to the server
   */
  
  //Data is to SEND hona h 
  function sendText(data){
      var json = JSON.stringify(data);
      webSocket.send(json);
  }
  
  function closeSocket(){
      webSocket.close();
  }
  
  ///jado response aaega MEANS jab data aaega
  function writeResponse(json){
  
      var response = JSON.parse(json);
      var output = response.data;
  </code></pre>
  
  <p>P.S: I am pretty sure there are no errors in the code. It looks only like a control flow problem to me.
  Any help will be deeply appreciated .Please consider asking for details if I missed out anything. </p>