2

I host my java/jsp based webapps using tomcat 7.0. I would like to make my webapps be able to invoke requests from other webapps (on the same machine). To do this, they need to know the url from which they can access each other. I don't want to do this hardcoded (either in the code, or in a config file), but I don't see how to let each webapp be aware of their own access-url. Specifically, I would like the webapps to be aware of the following information:

protocol: http or https host: my.machine.com port: 1234 webapp context: webappName

With this information They could build their access url: http://my.machine.com:1234/webappName/

Of course Tomcat knows this information, but how can I push this information into my java code? I was thinking about making a servletListener which stores this information in memory on startup of the app, but I only found a way to get the webapp context with this code:

public void contextInitialized(ServletContextEvent servletContextEvent)
{
    ServletContext servletContext = servletContextEvent.getServletContext();        
    String webappContext = servletContext.getContextPath();
}
user1884155
  • 3,616
  • 4
  • 55
  • 108
  • Yes, that's the only way you can know the base URL of the app, by using `ServletContext#getContextPath`. You should store this info in a cache shared by all your applications, like EhCache or Hazelcast. – Luiggi Mendoza Nov 26 '14 at 17:41
  • @LuiggiMendoza But that won't help with hostname or port. I could think of some hacks to get the hostname, but the port seems tricky. – Kayaman Nov 26 '14 at 17:47
  • @LuiggiMendoza That would return a file system path, not really helping at all (unless you'd go read the application server's config files through that). – Kayaman Nov 26 '14 at 17:55
  • @Kayaman oh right. AFAIK Servlet specification doesn't let you know the port number. I guess you could get the hostname by using the aprroach explained here: [Recommended way to get hostname in Java](http://stackoverflow.com/q/7348711/1065197). – Luiggi Mendoza Nov 26 '14 at 17:59
  • You can create a custom Valve implementation based on org.apache.catalina.valves.RequestFilterValve (or from scratch) and filter hostname, port, etc... via request attributes, then set and configure the valve at Host level container (or context, but is not neccesary). You do not need apply the check request logic per webapp, the Valve mechanism do this job in the same one point. – vzamanillo Nov 26 '14 at 21:30

1 Answers1

1

All the information can be obtained from HttpServletRequest.

Protocol:  request.getProtocol(); // Returns HTTP/1.1
Scheme: request.getScheme(); //returns http or https
Context: request.getContextPath(); // return webappname
Request URL: request.getRequestURL() //return http://my.machine.com:1234/webappName/servletpath
Request URI: request.getRequestUri() // returns /servletpath
Host: request.getLocalAddr(); // returns 0:0:0:0:0:0:0:1 (if server is running on localhost)
Port: request.getLocalPort(); // returns 1234
Hostname: request.getServerName(); // returns my.machine.com
Port: request.getServerPort(); // returns 8080
Bhaskara
  • 601
  • 9
  • 18
  • 1
    Additional: As OP is asking about getting these values during initialization it's worth pointing out that they're only available during request handling - the way you're documenting here. Naturally, a single host can have many different names, and there's no limit for the appserver on the number of ports that it makes available (though it's typically only one). – Olaf Kock Nov 26 '14 at 21:13
  • True, I had to revise my logic for this. There is no way my application can be aware of all its possible hosts without an actual request being received. So instead of on application startup, I changed my logic to trigger when the first request is received. – user1884155 Nov 27 '14 at 09:07