Alexander Shutyaev wrote:
Hi Christopher,
I've found a solution to my problem although I believe it's more accurate
to say I guessed it :) Maybe you'll be able to explain it to me. Here is
the complete code:
Tomcat tomcat = new Tomcat();
tomcat.setBaseDir(baseDir);
tomcat.getConnector().setPort(8080);
tomcat.getServer().setParentClassLoader(getClass().getClassLoader());
tomcat.getEngine(); // (1)
tomcat.init();
tomcat.getHost().addAlias("127.0.0.1"); // (2)
tomcat.start();
tomcat.addWebapp("", path);
(1) - this seems like it should be called from somewhere within Tomcat
class during initialization, but somehow it isn't called, and without this
line I get a NullPointerException
(2) - this is my solution to the problem I've described before; without
this line my app is only served on http://localhost:8080 but if I include
this line my app is available on http://localhost:8080,
http://127.0.0.1:8080 and all other assigned addresses like
http://192.168.1.1:8080 etc
Hi.
I have absolutely no idea how this really works, but I note that in server.xml, the
"default host" is an attribute of the <Engine> tag :
<Engine name="Catalina" defaultHost="localhost">
So maybe somehow by doing (1) above, the consequences are a bit deeper than just avoiding
the NullPointerException.
I don't find in the on-line documentation a description of how Tomcat exactly matches HTTP
requests to <Host> names, but the general logic in webservers is something like :
- consider the "Host:" header of the request
- try to match it to one of the defined hostnames in the server configuration
- if it matches, then handle this request as per the configuration of that
(virtual) host
- if it doesn't match, then handle the request with the configuration of whatever <Host>
is defined as the default Host.
In Apache httpd, if you have a single Host defined, it automatically becomes the default
Host. In Tomcat however, even if you have a single Host defined, it is not automatically
the default Host. You must explcitly name this default Host in the "defaultHost"
attribute of the <Engine> tag.
So maybe your (1) above initialises some internal structure which fills in the "default
host" value.
Anyway, if the Engine's defaultHost is set correctly (to "localhost" e.g.), then you
should not normally need to add the "127.0.0.1" Alias (as all requests should default to
that defaultHost anyway).
So to me, there is still something somewhat unsatisfactory in having to do (2)
above.
(But I am definitely not saying this as a Java or Tomcat/Java expert)
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
For additional commands, e-mail: users-h...@tomcat.apache.org