Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot connect to ServerEndpoint deployed in Tomcat

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, compiled 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 incomingMessage) {
        return "I recieved ('" + incomingMessage + "'), 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.com: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: enter image description here

(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: enter image description here

Here are the contents of catalina.out that are too vast to be included in this post: http://pastebin.com/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 compile 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 completed 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".

like image 348
RaminS Avatar asked Jul 12 '16 21:07

RaminS


1 Answers

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.

like image 56
Sanjeev Avatar answered Oct 29 '22 18:10

Sanjeev