Question: I have a java application run at **<https://example.com>** which is hosted on **server A**. When some access **<https://example.com/example>** it should redirect to the WordPress site which is hosted on **server B** but the URL should be the same **<https://example.com/example>**

Answer: 
I suggest you to use **`subdomain`** to do this.

ex- `https://example.example.com`
I'd run the java in a docker container, then use traefik to serve the main domain and the subdirectory from different containers or hosts.

Or you could use haproxy, then you probably would need to bind the java to another port.