最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

java - Cannot connect to ServerEndpoint deployed in Tomcat - Stack Overflow

programmeradmin1浏览0评论

A lot of people assume that I use an IDE, so let me state this right now: I do not use any IDE.


I took the following Java source code, piled it to Echo.class, then created Echo.war by writing jar -cvf Echo.war Echo.class in the Windows CMD, and uploaded the file inside $TOMCAT_HOME/webapps folder:

@ServerEndpoint("/echo")
public class Echo {

    @OnMessage
    public String echo(String iningMessage) {
        return "I recieved ('" + iningMessage + "'), so I am returning it.";
    }
}

After starting Tomcat, I suddenly have the following folder structure:

<webapps>
    ...
    <Echo.war>
    <Echo>
        <Echo.class>
        <META-INF>
            <MANIFEST.MF>
        </META-INF>
    </Echo>
</webapps>

When trying to open a websocket connection to my endpoint via JavaScript with new WebSocket("ws://example:8080/Echo/echo"), I get a 404 response instead of a 101 handshake.

If it is to any help, here is a picture of what the manager shows:

(Tomcat 8 update):

I updated to Tomcat 8 following this guide and now catalina.out is no longer empty and the manager now shows this:

Here are the contents of catalina.out that are too vast to be included in this post:

Echo.war is mentioned on lines 650, 651, 690, and 691.


I researched a bit and saw that if you build the class with Java 8 but your Tomcat is running on Java 7, you will get a UnsupportedClassVersionError. I didn't get that error, but I thought that I might as well update to Java 8 on my server. I did that and redeployed the WAR, but nothing changed.

I have also changed the annotation from @ServerEndpoint("/echo") to @ServerEndpoint("/dest") in case there was a name collision, but that didn't help either.


Here is a quote from the book I am reading:

Deploying your EchoServer WebSocket endpoint is particularly simple. You need to pile the source file, include the class file in the WAR file, and deploy the WAR file. The web container will detect that there is a WebSocket endpoint included in the WAR file and do the necessary setup to deploy it. Once you have pleted these steps, you are ready to make your first call to the WebSocket endpoint.

I took it that I have to create a WAR file with a single class file inside, but perhaps that is not the case, since he says "the WAR file" and not "a WAR file".

And according to this Wikipedia article, the WAR file has to contain a web.xml file (one might wonder why he didn't mention this). Is it true? Is this why it is not working? If it is, what should the web.xml contain? Surely it is not just an empty file with the name of "web.xml".

A lot of people assume that I use an IDE, so let me state this right now: I do not use any IDE.


I took the following Java source code, piled it to Echo.class, then created Echo.war by writing jar -cvf Echo.war Echo.class in the Windows CMD, and uploaded the file inside $TOMCAT_HOME/webapps folder:

@ServerEndpoint("/echo")
public class Echo {

    @OnMessage
    public String echo(String iningMessage) {
        return "I recieved ('" + iningMessage + "'), so I am returning it.";
    }
}

After starting Tomcat, I suddenly have the following folder structure:

<webapps>
    ...
    <Echo.war>
    <Echo>
        <Echo.class>
        <META-INF>
            <MANIFEST.MF>
        </META-INF>
    </Echo>
</webapps>

When trying to open a websocket connection to my endpoint via JavaScript with new WebSocket("ws://example.:8080/Echo/echo"), I get a 404 response instead of a 101 handshake.

If it is to any help, here is a picture of what the manager shows:

(Tomcat 8 update):

I updated to Tomcat 8 following this guide and now catalina.out is no longer empty and the manager now shows this:

Here are the contents of catalina.out that are too vast to be included in this post: http://pastebin./cwLviH5b

Echo.war is mentioned on lines 650, 651, 690, and 691.


I researched a bit and saw that if you build the class with Java 8 but your Tomcat is running on Java 7, you will get a UnsupportedClassVersionError. I didn't get that error, but I thought that I might as well update to Java 8 on my server. I did that and redeployed the WAR, but nothing changed.

I have also changed the annotation from @ServerEndpoint("/echo") to @ServerEndpoint("/dest") in case there was a name collision, but that didn't help either.


Here is a quote from the book I am reading:

Deploying your EchoServer WebSocket endpoint is particularly simple. You need to pile the source file, include the class file in the WAR file, and deploy the WAR file. The web container will detect that there is a WebSocket endpoint included in the WAR file and do the necessary setup to deploy it. Once you have pleted these steps, you are ready to make your first call to the WebSocket endpoint.

I took it that I have to create a WAR file with a single class file inside, but perhaps that is not the case, since he says "the WAR file" and not "a WAR file".

And according to this Wikipedia article, the WAR file has to contain a web.xml file (one might wonder why he didn't mention this). Is it true? Is this why it is not working? If it is, what should the web.xml contain? Surely it is not just an empty file with the name of "web.xml".

Share Improve this question asked Jul 12, 2016 at 21:19 RaminSRaminS 2,2394 gold badges23 silver badges31 bronze badges 8
  • Sorry for typo, try with /Echo/echo – Federico Sierra Commented Jul 13, 2016 at 1:58
  • Check the logs files, verify if your war file is deploying correctly. – Federico Sierra Commented Jul 13, 2016 at 2:18
  • <TOMCAT_HOME>/logs/catalina.out – Federico Sierra Commented Jul 13, 2016 at 2:20
  • What are you using to pile the war? – Ishmael Commented Jul 14, 2016 at 5:59
  • Could you try doing a jar tf Echo.war and sharing what the output looks like? – Ishmael Commented Jul 15, 2016 at 19:15
 |  Show 3 more ments

3 Answers 3

Reset to default 6 +50

Create Following file structure

WEB-INF
  |
   - classes
        |
         - Echo.class

And create your war file by jar -cvf echo.war * from the parent folder of WEB-INF. Deploy this war file to tomcat and try connecting using web-socket.

Remove javax.websocket library from the build of your WAR file.

If you're using maven set the dependency to provided. For example:

<!-- Provided Websocket API, because tomcat has its own implementation -->
<dependency>
    <groupId>javax.websocket</groupId>
    <artifactId>javax.websocket-api</artifactId>
    <version>1.1</version>
    <scope>provided</scope>
</dependency>

In Eclipse or IntelliJ you can set which libraries to export with your WAR.

Source: WebSocket handshake: Unexpected response code: 404 -

How are you piling the class? When you pile the class, dependent libraries must be in the class path. Those libraries must also be in WEB-INF/lib directory under the war deployment

发布评论

评论列表(0)

  1. 暂无评论