Websockets not working on deployed Tomcat Server on Google Compute Engine - java

totally new to deploying apps with websockets so I feel like I'm missing something. I'm using Tomcat 8.5 on a Debian 8 instance. Steps I've done to deploy it on GCE:
Used the Click to Deploy for Tomcat.
Uploaded my .war file onto the deployed site.
Promoted the instance's IP to static.
Added firewall rule on port 80 with the target 'websocket'
Made a load balancer on the instance group of the server's instance. (Not quite sure if that helps or if I set it up right)
The load balancer looks like this.
My javascript creates the websocket like this (VDMServer is the name of the .war):
var websocket = new WebSocket("ws://[EXTERNALIP]/VDMServer/gameServerEndpoint");
And here's how I made the server endpoint class:
#ServerEndpoint("/gameServerEndpoint")
public class GameServerEndpoint {
...
}
I am able to access the website on the virtual instance's external IP and the pages of my app, but even trying Tomcat's websocket examples give the error:
Error during WebSocket handshake: Unexpected response code: 404
Also here's how my Connecter in my server.xml looks like in /var/lib/tomcat8/conf:
<Connector port="80" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
Also seen solution about not including the javax websocket library so I made sure not to do that. Here's what my .war's lib folder looks like. That shouldn't affect the tomcat examples though right?
Any help on this case would be much appreciated, been looking for a way to fix this all day yesterday. Thanks!

In my case it worked when I deployed a war without exporting any sources file.

Related

How to add health endpoint in Apache Tomcat 9?

I am using Apache Tomcat 9 server as a Maven dependency in my project. It is working fine and now I need to add a health endpoint so that it will return 200 OK if everything is running fine.
I came to know about HealthCheckValve (https://tomcat.apache.org/tomcat-9.0-doc/config/valve.html#Health_Check_Valve) option in Tomcat 9 which is helpful. But I am not been able to figure out where to add this and the process of configuring this valve. As I know if server is standalone we can configure in Server.xml but as the Tomcat Server is a maven dependency I don't know how and where I should configure this.
Can somebody please help me in configuring health endpoint in Apache Tomcat 9 (as a maven dependency) ?
See the documentation, then add the HealthCheckerValve to server.xml. Valves go into either the Engine, Host or Context element. In the server.xml packaged with Tomcat you can find comments that should direct you to the right location.
When embedding a version of Tomcat, you won't have this file available, and so you need to assemble instances of these containers programmatically.
Check the launcher application in this example: https://www.oracle.com/webfolder/technetwork/tutorials/obe/java/basic_app_embedded_tomcat/basic_app-tomcat-embedded.html
While I could not find methods like addValve() I found an init() method that you could use to provide a server.xml which will be read by Tomcat.
I saw the documentation of all valves available in Tomcat 9.0.x.
In order to find the solution of this specific task, I tried looking for configuration of other valves such as Remote Address Valve in embedded tomcat.
I found a solution by user967710 after searching a lot.
I did the following to add a Health Check Valve to my Tomcat 9.0.64 :
Tomcat tomcat = new Tomcat();
tomcat.getEngine().setName(UUID.randomUUID().toString());
tomcat.setPort(context.port);
tomcat.setHostname(context.hostname);
tomcat.getHost().setAppBase(".");
Valve valve = new HealthCheckValve();
tomcat.getHost().getPipeline().addValve(valve);
It doesn't matter how you configure the Tomcat for your project i.e from line 1 ~ 5 but actually last 2 lines i.e 6 and 7 are important where you are adding the valve.
The health endpoint can be accessible on host:port/health.
For e.g if it is hosted at http://localhost:4000 then the health endpoint would be http://localhost:4000/health
This endpoint will return 200 OK with a simple JSON response stating the Tomcat server status i.e "UP" if everything is up and running.

Tomcat Deployment and Routing

I feel like I’m attempting to accomplish a pretty simple task but I’m stumped. I’m attempting to use the Tomcat Manage App to deploy my app and then route my root domain name to point to that deployment.
Specifically, the app currently deployed at http://www.schmud.de/home/ should load when a person types http://www.schmud.de into their browser.
I tried deploying and configuring this in CPanel with no luck. It seems that the Tomcat Manage App interface or Host Manager interface is what I’m supposed to be use to configure my routing?
My .htaccess currently looks like this:
SetHandler jakarta-servlet
SetEnv JK_WORKER_NAME ajp13
My web.xml is generated by Clojure.
There is no way to do it within the Tomcat Web Interface nor the .htaccess file. The user must have deeper access on a CPanel system.
Assuming that you have deployed your application as "app" and configured your .htaccess as I described above, here are the specific instructions I gave my root admin to solve the routing issue:
open /usr/local/easy/share/easy-tomcat7/conf/server.xml
Under the <Host> tag add this:
<Context path="" docBase=“app” debug="0" reloadable="true" />
Restart the server
Upon restart, http://domain.com/ should load my homepage (just as http://domain.com/app/ does now)

Tomcat Webapp on port 80 [duplicate]

This question already has answers here:
How to change the port of Tomcat from 8080 to 80?
(14 answers)
Closed 9 years ago.
I have a webapp on my tomcat server like this:
mydomain.com:8080/mywebapp
Then I connect to my webapp, and it is working correctly, but what I want is to see my webapp like this:
mydomain.com
So I don't want only tomcat on port 80, I don't want to access my webapp through its name, I want to connect directly using my domain URI.
How can I do this? I want this to work with Linux (Ubuntu 12.04 LTS) and Windows servers.
There are several ways to achieve this, but the most common way to solve it is to run Apache as a reverse proxy in front of it. You can find some details here. This will work on both Linux and Windows. For Linux, you can also use authbind to allow Tomcat to bind to port 80.
Just changing the port to 80 in your server.xml will not work in Linux, since it would require you to start Tomcat as root, which is not a very good idea.
Also, to have your webapp at /, you can deploy your war file as ROOT.war.
Running any application on a privileged port (those below 1024) requires special privileges. If you do this, you should ensure your instance is properly hardened.
To configure the port tomcat listens on you have to modify the HTTP connector in conf/server.xml (server reference documentation):
<Connector port="80" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
To change the context path of an app, you can rename the war file. To deploy it at the root, rename your war file to ROOT.war. Or you can add a META-INF/context.xml in which you can specify the desired context path (context reference docs):
<?xml version="1.0" encoding="UTF-8"?>
<Context path="/" />
You need to set apache webserver and configure it to use tomcat.
You need to use mod_jk in order to configure apache webserver to communicate with tomcat.
Use this link to set up the mod_jk.

Map different url to same web application in Tomcat

I am not clear on the following:
If we have a web application named: SomeWebApp under Tomcat's webapp directory, the url to access it is:
http://localhost:8080/SomeWebApp
My question is, is it possible to configure Tomcat so that other URLs would point to that web application?
E.g.
http://localhost:8080/ADifferentApp will also point to the SomeWebApp?
From the web.xml I think is not possible since it is about the url patterns when you are inside the SomeWebApp scope.
So what is the proper way to do it? If it is possible that is.
The approach I found to work best is to install Apache2 on the server and proxy all requests. Tomcat is surprisingly difficult to configure in other ways than intended. In my experience, Tomcat doesn't provide this functionality declaratively.
I'd rather recommend Nginx than Apache as proxy. I'm recently working on a project that incorporates tomcat and nginx works as proxy.
Once you've got nginx you can acctualy map as many url's to access the same web application as you want.
Yes,its possible to map different context path to single application edit conf/server.xml file
> **> <Context docBase="D:\Servers\apache-tomcat-7\webapps\SomeWebApp"
> > path="/SomeWebApp" />
> > <Context docBase="D:\Servers\apache-tomcat-7\webapps\SomeWebApp" path="/ADifferentApp "/>**
Access application with 2 URL's

Short url or alias for deployed application in tomcat 6

I have a web application project which is deployed in tomcat 6.
I can access my application using the url:
http://localhost:8082/MyApplication
I also wan't to be able to access this application by another url like:
http://localhost:8082/myapp
Is this possible ? if yes what alternatives do i have ?
Off course, I don't want to change the original name of the application('MyApplication').
Thanks,
Abhishek.
If you add the Context within server.xml it will work as you want. Give the path attribute you wish.
<Context docBase="MyApplication" path="/myapp" />
Though it works, this approach is not recommended by the Tomcat docs, since any changes to server.xml means restarting the server disturbing all the web apps.
But, on the flip side, the practice of keeping this in Catalina_Home/conf/Catalina/localhost/context.xml (which is recommended by the docs) has some unreliabilities as others have reported - when you redeploy the war you can lose the context.xml too
See Why-does-tomcat-replace-context-xml-on-redeploy and
Why does tomcat like deleting my context.xml file?

Categories