I’m trying to write my first client-server application using WebSocket and Tomcat 9 server. I found this example on the Internet. Tomcat gives a JSP file, everything is simplified, but specifically in 17 line,
var websocket = new websocket ("WS: // Localhost: 8080 / [NameoftheClassWithwebsocket] / WS");
Where the WebSocket is initialized – an error occurs:
“(index): 17 WebSocket Connection to ‘WS: // LocalHost: 8080 / Javawebsocket / WS’
Failed: Error During Websocket Handshake: UNExpected Response Code: 404 “
tried many tutorials to check, in the result – came here with a question. Java code and JSP code are presented below.
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 WebSocketServerExampleFle {
@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;
}
@ Merror
Public Void Onerror (Throwable E) {
E.PrintStackTrace ();
}
}
jsp file:
& lt;% @ page contenttype = "text / html; charset = utf-8" Language = "java"% & gt;
& lt; html & gt;
& lt; Head & gt;
& lt; Meta Charset = "UTF-8" & GT;
& lt; Title & gt; Tomcat WebSocket & LT; / Title & GT;
& lt; / Head & gt;
& lt; Body & gt;
& lt; Form & gt;
& lt; Input id = "Message" Type = "Text" & gt;
& lt; input onClick = "wssendMessage ();" Value = "Echo" Type = "Button" & gt;
& lt; input onClick = "wscloseconnection ();" Value = "Disconnect" type = "Button" & gt;
& lt; / form & gt;
& lt; br & gt;
& lt; textarea id = "echotext" rows = "5" cols = "30" & gt; & lt; / textarea & gt;
& lt; script type = "text / javascript" & gt;
Var Websocket = New WebSocket ("WS: // Localhost: 8080 / Javawebsocket / WS");
var echotext = document.gelementByid ("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";
}
& lt; / script & gt;
& lt; / body & gt;
& lt; / html & gt;
Answer 1, Authority 100%
The solution was found. The problem was that the context, when loaded through the Intellij IDEA, the Tomcat server was localhost: 8080 , and in the example, in the most ill-fated line:
var websocket = new websocket ("WS: // Localhost: 8080 / javawebsocket / ws");
Where Javawebsocket is the name of the project, the context was – Localhost: 8080 / javawebsocket.
If I started through Tomcat, pre-move all the files to the server, then, yes, the example would have worked correctly, but T .. I started it through the IDEA, then the context was incorrect and, accordingly, there was an error.
those. All that required is to change the problem line to
var websocket = new websocket ("WS: // LocalHost: 8080 / WS");