Eclipse WebSocket-“网络日志”;WebSocket握手时出错:意外响应代码:404“;将外部jar添加到项目中时

Eclipse WebSocket-“网络日志”;WebSocket握手时出错:意外响应代码:404“;将外部jar添加到项目中时,eclipse,tomcat,jakarta-ee,jar,websocket,Eclipse,Tomcat,Jakarta Ee,Jar,Websocket,我正在使用Eclipse和apache 7.x使用WebSocket构建简单的聊天应用程序。但是我无法编译我的代码,因为它显示导入javax.WebSocket无法解析。在谷歌搜索了很多之后,我发现在应用程序中显示了外部jar。我在我的库部分添加了一个jar。它删除了我当前的错误,但给出了另一个错误。现在它显示到“ws://localhost:8080/WebSocket/socket”的WebSocket连接失败:WebSocket握手期间出错:意外响应代码:404 我尝试了很多方法,比如添

我正在使用Eclipse和apache 7.x使用WebSocket构建简单的聊天应用程序。但是我无法编译我的代码,因为它显示导入javax.WebSocket无法解析。在谷歌搜索了很多之后,我发现在应用程序中显示了外部jar。我在我的库部分添加了一个jar。它删除了我当前的错误,但给出了另一个错误。现在它显示到“ws://localhost:8080/WebSocket/socket”的WebSocket连接失败:WebSocket握手期间出错:意外响应代码:404


我尝试了很多方法,比如添加servlet-api.jar来从ApacheLib构建路径。还可以找到一些信息,比如,它还提供了javax.websocket-api-1.0.jar库,因此应用程序中不需要这个库。您可能在编译时需要它,但服务器将在运行时提供它

现在我陷入了递归。当我移除我的jar时,出现了第一个错误,当我添加外部jar时,出现了第二个错误。请在这个问题上帮助我,因为我们正在为实时协作编辑器开发迷你项目,我们打算在其中使用websocket


下面是我的websocket代码(不使用maven):

WebSocket.java

package com.psl.service;

import java.io.IOException;
import java.io.StringWriter;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;

import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONWriter;


@ServerEndpoint("/socket")
public class WebSocket {
Set<Session> sessionSet = Collections.synchronizedSet(new HashSet<>());

@OnOpen
public void onOpen(Session webSession) {
    System.out.println("Opened");
    sessionSet.add(webSession);
}

@OnMessage
public void onMessage(String message, Session webSession) throws IOException {
    System.out.println("Got message    "+message);
    String username = (String)webSession.getUserProperties().get("username");
    if(username==null) {
        webSession.getUserProperties().put("username", message);
        webSession.getBasicRemote().sendText(buildJSON(username, message));
    }
    else {
        for(Session s : sessionSet) {
            s.getBasicRemote().sendText(buildJSON(username, message));
        }
    }
}


private String buildJSON(String string, String string2) {
    String jsonData="";
    JSONObject obj=new JSONObject();
      try {
        obj.put("message",string + ":" + string2);
        StringWriter out = new StringWriter();

          JSONWriter jwriter = new JSONWriter(out);
          jwriter.key(string).value(string2);
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

      return null;
}
@OnClose
public void onClose() {
    System.out.println("CLosed");
    sessionSet.remove(sessionSet);
}

}
package com.psl.service;
导入java.io.IOException;
导入java.io.StringWriter;
导入java.util.Collections;
导入java.util.HashSet;
导入java.util.Set;
导入javax.websocket.OnClose;
导入javax.websocket.OnMessage;
导入javax.websocket.OnOpen;
导入javax.websocket.Session;
导入javax.websocket.server.ServerEndpoint;
导入org.json.JSONException;
导入org.json.JSONObject;
导入org.json.JSONWriter;
@ServerEndpoint(“/socket”)
公共类WebSocket{
Set sessionSet=Collections.synchronizedSet(newhashset());
@奥诺彭
公共打开(会话Web会话){
系统输出打印项次(“打开”);
session.add(webSession);
}
@OnMessage
public void onMessage(字符串消息、会话webSession)引发IOException{
System.out.println(“获取消息”+消息);
字符串用户名=(字符串)webSession.getUserProperties().get(“用户名”);
如果(用户名==null){
webSession.getUserProperties().put(“用户名”,消息);
getBasicRemote().sendText(buildJSON(用户名、消息));
}
否则{
用于(会话s:会话开始){
s、 getBasicRemote().sendText(buildJSON(用户名、消息));
}
}
}
私有字符串buildJSON(字符串、字符串2){
字符串jsonData=“”;
JSONObject obj=新的JSONObject();
试一试{
对象放置(“消息”,字符串+”:“+string2);
StringWriter out=新建StringWriter();
JSONWriter jwriter=新的JSONWriter(out);
jwriter.key(string).value(string2);
}捕获(JSONException e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
返回null;
}
@一次
公开作废{
系统输出打印项次(“关闭”);
sessionstart.remove(sessionstart);
}
}
chat.html

    <!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>

<script type="text/javascript">
    var websocket = new WebSocket("ws://localhost:8080/WebSocketDemo/socket");

    websocket.onmessage = function onMessage(message) {
        var jsonData = JSON.parse(message.data);
        if(jsonData.message != null) {
            messageTextArea.value += jsonData.message + "\n";
        }
    }
    function sendMessage() {
        websocket.send(sendText.value);
        sendText.value = "";
    }
</script>
</head>
<body>

<textarea rows="30" cols="70" id="messageTextArea" ></textarea><br /><br />
<input id="sendText">
<input type="button" value="Send" onclick="sendMessage()">


</body>
</html>

在此处插入标题
var websocket=newwebsocket(“ws://localhost:8080/WebSocketDemo/socket”);
websocket.onmessage=函数onmessage(消息){
var jsonData=JSON.parse(message.data);
if(jsonData.message!=null){
messageTextArea.value+=jsonData.message+“\n”;
}
}
函数sendMessage(){
send(sendText.value);
sendText.value=“”;
}



我得到了这个问题的答案。这是因为添加的jar覆盖了ApacheTomcat中的内部jar。我使用的是没有websocketapi.jar的旧tomcat


所以我对这个问题的解决方案是要么使用Glashfish4.x,要么使用ApacheTomcat8.x,它为我们提供了运行websocket所需的jar。不需要添加任何额外的websocket api jar。

我得到了这个查询的答案。这是因为添加的jar覆盖了ApacheTomcat中的内部jar。我使用的是没有websocketapi.jar的旧tomcat

所以我对这个问题的解决方案是要么使用Glashfish4.x,要么使用ApacheTomcat8.x,它为我们提供了运行websocket所需的jar。无需添加任何额外的websocket api jar