Tomcat 9 and websocket. Got a 404 error. What is wrong in my code?

Viewed 692

I'm t rying to make my the first simple client-servet application with web socket and tomcat 9 server. I found this example in the enternet. I had the next error:

(index):17 WebSocket connection to 'ws://localhost:8080/JavaWebSocket/ws' failed: 

Error during WebSocket handshake: Unexpected response code: 404"

it's this line:

var webSocket = new WebSocket("ws://localhost:8080/JavaWebSocket/ws")" 

I thought the initialisation should be

 var webSocket = new WebSocket("ws://localhost:8080/[nameOfTheClassWithWebSocket]/ws");

But it doesn't work too :( I tried everything :( I can't solve this problem :(

Java code and Jsp file with project scheme are bellow.

Java code:

package socket;
import javax.websocket.OnClose;
import javax.websocket.OnError;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.server.ServerEndpoint;

@ServerEndpoint("/ws")
public class WebSocketServerExample {
@OnOpen
public void onOpen(){
    System.out.println("Open Connection ...");
}

@OnClose
public void onClose(){
    System.out.println("Close Connection ...");
}

@OnMessage
public String onMessage(String message){
    System.out.println("Message from the client: " + message);
    String echoMsg = "Echo from the server : " + message;
    return echoMsg;
}

@OnError
public void onError(Throwable e){
    e.printStackTrace();
}
}

jsp file:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
  <meta charset="UTF-8">
  <title>Tomcat WebSocket</title>
</head>
<body>
<form>
  <input id="message" type="text">
  <input onclick="wsSendMessage();" value="Echo" type="button">
  <input onclick="wsCloseConnection();" value="Disconnect" type="button">
</form>
<br>
<textarea id="echoText" rows="5" cols="30"></textarea>
<script type="text/javascript">
    var webSocket = new WebSocket("ws://localhost:8080/JavaWebSocket/ws");
    var echoText = document.getElementById("echoText");
    echoText.value = "";
    var message = document.getElementById("message");
    webSocket.onopen = function(message){ wsOpen(message);};
    webSocket.onmessage = function(message){ wsGetMessage(message);};
    webSocket.onclose = function(message){ console.log(message);};
    webSocket.onerror = function(message){ console.log(message);};
    function wsOpen(message){
        echoText.value += "Connected ... \n";
    }
    function wsSendMessage(){
        webSocket.send(message.value);
        echoText.value += "Message sended to the server : " + message.value + "\n";
        message.value = "";
    }
    function wsCloseConnection(){
        webSocket.close();
    }
    function wsGetMessage(message){
        echoText.value += "Message received from to the server : " + message.data + "\n";
    }
    function wsClose(message){
        echoText.value += "Disconnect ... \n";
    }

    function wserror(message){
        echoText.value += "Error ... \n";
    }
</script>
</body>
</html>

Scheme of the Project

1 Answers

Your url will be var webSocket = new WebSocket("ws://localhost:[port]/appcontext/ws")"

Related