Friday, March 22, 2013

Tomcat Tips

Getting your server name can be a tricky question because tomcat may be on box1 behind box2. What you probably care about is how the client addressed you, i.e. box1. That information should be included in the HOST header of the request. Here's the output of various HttpServletRequest functions when the browser asked for who was apache and who proxied the request to (which is unresolvable to the browser).

value in browser url box:
actual resource served:

HttpServletRequest.getRequestURI()   = /hello/world
HttpServletRequest.getRequestURL()   =
HttpServletRequest.getServerName()   =
HttpServletRequest.getHeader("HOST") =
HttpServletRequest.getLocalName()    =

From HttpUtils.getRequestURL (which I believe is what is actually called by HttpServletRequest.getRequestURL) you can see that host part of getRequestURL is just the result of getServerName. The implementation of getServerName is totally opaque to me but one would hope it was a getHeader("HOST") followed by some system call when absent. According to spec it is the value of the part before ":" in the Host header value, if any, or the resolved server name, or the server IP address.

{ "loggedin": false, "owner": false, "avatar": "", "render": "nothing", "trackingID": "UA-36983794-1", "description": "", "page": { "blogIds": [ 415 ] }, "domain": "", "base": "\/michael", "url": "https:\/\/\/michael\/", "frameworkFiles": "https:\/\/\/michael\/_framework\/_files.4\/", "commonFiles": "https:\/\/\/michael\/_common\/_files.3\/", "mediaFiles": "https:\/\/\/michael\/media\/_files.3\/", "tmdbUrl": "http:\/\/\/", "tmdbPoster": "http:\/\/\/t\/p\/w342" }