Unable to connect to ServerEndpoint deployed to Tomcat

Many people assume that I am using an IDE, so let me say it right now: I am not using an IDE.


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

 @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 got the following folder structure:

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

When I try to open a connection to my endpoint via JavaScript using new WebSocket("ws://example.com:8080/Echo/echo") , I get a 404 response instead of 101 handshakes.

If this is any help, here is an image of what the manager shows: enter image description here

(Tomcat 8 update):

I updated Tomcat 8 after this tutorial and now catalina.out no longer empty, and the manager now shows this: enter image description here

Here is the content of catalina.out , which is too large to be included in this post: http://pastebin.com/cwLviH5b

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


I studied a little and saw that if you create a class with Java 8, but Tomcat will work in Java 7, you will get UnsupportedClassVersionError . I did not get this error, but I thought that I could also upgrade Java 8 on my server. I did this and translated WAR, but nothing has changed.

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


Here is a quote from a book that I am reading :

Deploying an EchoServer WebSocket endpoint is especially easy. 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 configure as necessary to deploy it. After you complete these steps, you are ready to make your first call to the WebSocket endpoint.

I realized that I needed to create a WAR file with one class file inside, but maybe it is not, because it says “WAR file” and not “WAR WAR”.

And according to this Wikipedia article , the WAR file should contain the web.xml file (one might wonder why he didn’t mention this). It's true? Does it really not work? If so, what should web.xml contain? Of course, this is not just an empty file called "web.xml".

+6
source share
3 answers

Create the following file structure

 WEB-INF | - classes | - Echo.class 

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

+6
source

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

If you are using maven, set the dependency to provide. For instance:

 <!-- 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 from your WAR.

Source: WebSocket Handshake: Unexpected Response Code: 404 -

0
source

How do you compile a class? When you compile a class, the dependent libraries must be in the class path. These libraries should also be in the WEB-INF / lib directory under military deployment.

0
source

All Articles