I have two applications deployed in a JBoss container (same unix box). If I receive a request from app1, I need to send a corresponding request for app2.
An Example: If app1 requests:
then I need to extract
http://example.com/, so that I can send the request for the second app.
I tried using
HttpServletRequest.getServerName() & HttpServletRequest.getServerPort() & HttpServletRequest.getHeader("host")
but how can I destinguish between
You can use
HttpServletRequest.getScheme() to retrieve either “http” or “https”.
Using it along with
HttpServletRequest.getServerName() should be enough to rebuild the portion of the URL you need.
You don’t need to explicitly put the port in the URL if you’re using the standard ones (80 for http and 443 for https).
Edit: If your servlet container is behind a reverse proxy or load balancer that terminates the SSL, it’s a bit trickier because the requests are forwarded to the servlet container as plain http. You have a few options:
HttpServletRequest.getHeader("x-forwarded-proto")instead; this only works if your load balancer sets the header correctly (Apache should afaik).
Configure a RemoteIpValve in JBoss/Tomcat that will make
getScheme()work as expected. Again, this will only work if the load balancer sets the correct headers.
If the above don’t work, you could configure two different connectors in Tomcat/JBoss, one for http and one for https, as described in this article.