You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Garret Wilson <ga...@globalmentor.com> on 2019/04/19 17:24:13 UTC
unable to serve static files with embedded Tomcat
Embedding Tomcat 9 (with OpenJDK 11 on Windows 10) I want to serve
static files from `/foo/bar`. From scouring the web (primarily
https://stackoverflow.com/a/15235711/421049 ), the documentation, and
some books (primarily _Apache Tomcat 7_), I have this:
Tomcat tomcat = new Tomcat();
tomcat.setPort(8080);
tomcat.setBaseDir("/foo");
Context ctx = tomcat.addContext("", "/foo/bar");
final Wrapper defaultServlet = ctx.createWrapper();
defaultServlet.setName("default");
defaultServlet.setServletClass("org.apache.catalina.servlets.DefaultServlet");
defaultServlet.addInitParameter("debug", "1");
defaultServlet.addInitParameter("listings", "false");
defaultServlet.setLoadOnStartup(1);
ctx.addChild(defaultServlet);
ctx.addServletMappingDecoded("/", "default");
ctx.addWelcomeFile("index.html");
tomcat.start();
tomcat.getServer().await();
Everything looks like it starts up:
Apr 19, 2019 2:18:09 PM org.apache.catalina.core.StandardService
startInternal
INFO: Starting service [Tomcat]
Apr 19, 2019 2:18:09 PM org.apache.catalina.core.StandardEngine
startInternal
INFO: Starting Servlet engine: [Apache Tomcat/9.0.19]
Apr 19, 2019 2:18:10 PM
org.apache.catalina.util.SessionIdGeneratorBase createSecureRandom
WARNING: Creation of SecureRandom instance for session ID
generation using [SHA1PRNG] took [281] milliseconds.
Apr 19, 2019 2:18:10 PM org.apache.catalina.core.ApplicationContext log
INFO: default: DefaultServlet.init: input buffer size=2048, output
buffer size=2048
But connections to http://localhost:8080/ time out. (I'm pretty sure but
not positive that I don't have anything blocking this in the firewall.)
Is there some simple thing I'm missing to connect things together?
Thanks,
Garret
P.S. The documentation on the web for the details of this is
surprisingly sparse.
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org
Re: unable to serve static files with embedded Tomcat
Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256
Garret,
On 4/19/19 19:18, Garret Wilson wrote:
> On 4/19/2019 8:04 PM, Rémy Maucherat wrote:
>> On Fri, Apr 19, 2019 at 11:14 PM Garret Wilson
>> <ga...@globalmentor.com> wrote:
>>
>>> On 4/19/2019 3:24 PM, Rémy Maucherat wrote:
>>>
>> tomcat.getService().addConnector(new Connector()); works very
>> well, etc, just look at what getConnector() does, it's very
>> simple.
>
> I did look at that first; that's why I mentioned the magic strings
> such as "HTTP/1.1". If I just use `new Connector()`, I may get
> something different than if I say `new Connector("HTTP/1.1")`. The
> latter potentially takes advantage of the APR connector if present;
> the former does not. (And how would I know that without looking at
> the source code?)
>
> I'm happy to call
> `tomcat.getSerivce().addConnector(defaultConnector)`, but there
> needs to be some method for creating a connector with a default
> configuration. Otherwise I'm copying more and more large hunks of
> code from the Tomcat source code, which may break when things
> diverge in the future (not to mention cutting down on
> reusability).
>
It also seems reasonable to be able to create two connectors with the
default configuration where the only difference is the port number.
Something like:
Connector connector = tomcat.createDefaultConnector();
connector.setPort(8443);
tomcat.getService().addConnector(connector);
connector = tomcat.createDefaultConnector();
connector.setPort(8444);
tomcat.getService().addConnector(connector);
This is not currently possible unless the Connector is (unlikely)
cloneable.
>> You can also use a server.xml (and some other important config
>> files like web.xml) for your embedded now, you can look at
>> Tomcat.main(...) (it is used by the Tomcat container image
>> example). If your configuration becomes complex, it could be
>> better than using Java to configure Tomcat.
>
>
> I don't want server.xml or web.xml. This is /not/ complex; I merely
> want to serve static files in a directory. The irony is that I
> thought I was simplifying things my removing JSP etc. support; it
> turns out that this "simplification" complicated things greatly and
> required me to copy big hunks of Tomcat code. This could and should
> be improved, and I'm happy to help improve it.
Patches are always welcome. I'd submit separate patches each step of
the way instead of one huge patch that makes the Tomcat class look the
way you want it.
I'd start with refactoring the code that creates a default connector
into a separate method and then wiring it into the getConnector()
method where it creates a default connector. That may be all you
really need.
- -chris
-----BEGIN PGP SIGNATURE-----
Comment: Using GnuPG with Thunderbird - https://www.enigmail.net/
iQIzBAEBCAAdFiEEMmKgYcQvxMe7tcJcHPApP6U8pFgFAly9wq4ACgkQHPApP6U8
pFhnqw//eAo1s52+6XN60iFiMpbVgPEU0UjFFQeYIvelk9B/kb61gZafD9+PNNMh
AixE1NC+RUymUSByFg3kSjiiDn8S5rmlEcpe0q1j/v0rlrU+hdnEAhHf4QqtKgXV
anT24kKhOoavI2GHAq8b+AeiAOcCpqxFNFKmlpIXORALzBN2Loks7FGMmet/O9vd
WuCeqLwBMU61szbXvzndMt157XOodhcAL77GjCeK25EC/LUC3o0YMq/ZVJGZwKV3
OXxR8pbCsPN1SNySEEX5eMPWAKcqwYskBg3Up2gKXnrgbRDq4k1SQ+Wv0qqnrzJC
MuJ+IcmUTxbZ9UL4lG1SXGQbZAnwrUCbIK9RKYVdE3h9ak46eXbUJRK79nmBhqlT
33dplH5rvK0A3RY230GX0z3XTyQbJT3qA6fjTs00wEr5onQNIzluvtYEIQVy6/gz
l41LgD9+QVMaPTSY+LGmoYhydNN/Lyb9z5VYeff56m9fPTBIVsmxK+bpauM0UQnW
tapVujRNfbqA0fvTKkrGJPkbzdYP59woWRyEMLPE5j6Ice+c1QHNmRSW+zDB716q
oEWq/ihHrkyK8GUWiUcgam/3zhehVdDFsfwTgFatPKdVYtRUxoG4AyHYnoEA7OWG
tXmgPrHrj4LThSDOrtnb4MKzh4Tp5JNOrVerPpfzaMxtjaLWQ1I=
=Zi/P
-----END PGP SIGNATURE-----
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org
Re: unable to serve static files with embedded Tomcat
Posted by Garret Wilson <ga...@globalmentor.com>.
On 4/19/2019 8:04 PM, Rémy Maucherat wrote:
> On Fri, Apr 19, 2019 at 11:14 PM Garret Wilson <ga...@globalmentor.com>
> wrote:
>
>> On 4/19/2019 3:24 PM, Rémy Maucherat wrote:
>>
> tomcat.getService().addConnector(new Connector()); works very well, etc,
> just look at what getConnector() does, it's very simple.
I did look at that first; that's why I mentioned the magic strings such
as "HTTP/1.1". If I just use `new Connector()`, I may get something
different than if I say `new Connector("HTTP/1.1")`. The latter
potentially takes advantage of the APR connector if present; the former
does not. (And how would I know that without looking at the source code?)
I'm happy to call `tomcat.getSerivce().addConnector(defaultConnector)`,
but there needs to be some method for creating a connector with a
default configuration. Otherwise I'm copying more and more large hunks
of code from the Tomcat source code, which may break when things diverge
in the future (not to mention cutting down on reusability).
> …
> You can also use a server.xml (and some other important config files like
> web.xml) for your embedded now, you can look at Tomcat.main(...) (it is
> used by the Tomcat container image example). If your configuration becomes
> complex, it could be better than using Java to configure Tomcat.
I don't want server.xml or web.xml. This is /not/ complex; I merely want
to serve static files in a directory. The irony is that I thought I was
simplifying things my removing JSP etc. support; it turns out that this
"simplification" complicated things greatly and required me to copy big
hunks of Tomcat code. This could and should be improved, and I'm happy
to help improve it.
Garret
Re: unable to serve static files with embedded Tomcat
Posted by Rémy Maucherat <re...@apache.org>.
On Fri, Apr 19, 2019 at 11:14 PM Garret Wilson <ga...@globalmentor.com>
wrote:
> On 4/19/2019 3:24 PM, Rémy Maucherat wrote:
> > On Fri, Apr 19, 2019 at 7:46 PM Woonsan Ko <wo...@apache.org> wrote:
> >
> >> I found this before from
> >>
> https://stackoverflow.com/questions/48998387/code-works-with-embedded-apache-tomcat-8-but-not-with-9-whats-changed/49011424
> >>
> >> // Note: make sure that tomcat creates the default connector...
> >> tomcat.getConnector();
> >>
> >> Worth trying...
>
>
> Yes, that fixed it, Woonsan! Thank you so much!
>
>
> > That looks correct, and it's easy to spot with the logging (the connector
> > logs that it's starting if it's there). I removed the magic creation of
> the
> > connector because it was causing far more hacks (where the magic
> connector
> > had to be removed after its startup). It seemed more normal to have to
> > explicitly create a connector.
> >
> > Rémy
>
> I don't know the history of this, but I have a couple of doubts on the
> face of it.
>
> First, the impression I get from reading _Apache Tomcat 7_ (Apress) is
> that I can create a server piecing together the components (Server,
> Service, Connector, Engine, Host, Context) if I want, but the whole
> point of the `Tomcat` class is to be a helper for doing all this
> automatically. So it seems a bit countertuitive that the `Tomcat` class
> is now making me do some of this wiring automatically. Why then would I
> use the `Tomcat` class? Why don't I just wire it all together from
> scratch? And why did we arbitrarily pick `Connector` for this "magic"
> creation? Why don't we have to call `Tomcat.getHost()` to make sure the
> host gets built, for instance?
>
> Secondly, calling a getter method in order to invoke some secret magic
> creation sauce behind the scenes reeks of a kludge. (Nothing personal;
> I'm just commenting on the design.) This is not "normal". It's not at
> all obvious that there is this magic step required before the thing will
> work. For example, when you see the following code, without some blatant
> comment it's easy to think that this is a no-op call that should be
> removed:
>
> tomcat.setPort(8080);
> tomcat.getConnector(); //"It doesn't look like this does anything;
> maybe it we should remove it… "
> tomcat.setBaseDir("/foo/bar");
>
> If I'm forced to create a connector, I would at least like the option to
> do something normal and expected, such as:
>
> tomcat.getService().addConnector(tomcat.createDefaultConnector());
>
> I _could_ create a default connector manually, but I'd have to be
> copying more of the source code (along with "magic" identifiers such as
> "HTTP/1.1"). So returning to the first doubt above: why am I using the
> `Tomcat` class if it doesn't do the default things for me automatically?
>
Sorry, but the SpringBoot use case is more important [they want to be able
to create a Tomcat without a connector], so that's the way it is now.
tomcat.getService().addConnector(new Connector()); works very well, etc,
just look at what getConnector() does, it's very simple.
The Tomcat class is not supposed to help much in this area, but rather on
setting up the Tomcat instance right, creating contexts right, etc. You can
look at the code and you'll see what the more complex parts are. Adding a
connector isn't one of them.
You can also use a server.xml (and some other important config files like
web.xml) for your embedded now, you can look at Tomcat.main(...) (it is
used by the Tomcat container image example). If your configuration becomes
complex, it could be better than using Java to configure Tomcat.
Rémy
Re: unable to serve static files with embedded Tomcat
Posted by Garret Wilson <ga...@globalmentor.com>.
On 4/19/2019 3:24 PM, Rémy Maucherat wrote:
> On Fri, Apr 19, 2019 at 7:46 PM Woonsan Ko <wo...@apache.org> wrote:
>
>> I found this before from
>> https://stackoverflow.com/questions/48998387/code-works-with-embedded-apache-tomcat-8-but-not-with-9-whats-changed/49011424
>>
>> // Note: make sure that tomcat creates the default connector...
>> tomcat.getConnector();
>>
>> Worth trying...
Yes, that fixed it, Woonsan! Thank you so much!
> That looks correct, and it's easy to spot with the logging (the connector
> logs that it's starting if it's there). I removed the magic creation of the
> connector because it was causing far more hacks (where the magic connector
> had to be removed after its startup). It seemed more normal to have to
> explicitly create a connector.
>
> Rémy
I don't know the history of this, but I have a couple of doubts on the
face of it.
First, the impression I get from reading _Apache Tomcat 7_ (Apress) is
that I can create a server piecing together the components (Server,
Service, Connector, Engine, Host, Context) if I want, but the whole
point of the `Tomcat` class is to be a helper for doing all this
automatically. So it seems a bit countertuitive that the `Tomcat` class
is now making me do some of this wiring automatically. Why then would I
use the `Tomcat` class? Why don't I just wire it all together from
scratch? And why did we arbitrarily pick `Connector` for this "magic"
creation? Why don't we have to call `Tomcat.getHost()` to make sure the
host gets built, for instance?
Secondly, calling a getter method in order to invoke some secret magic
creation sauce behind the scenes reeks of a kludge. (Nothing personal;
I'm just commenting on the design.) This is not "normal". It's not at
all obvious that there is this magic step required before the thing will
work. For example, when you see the following code, without some blatant
comment it's easy to think that this is a no-op call that should be removed:
tomcat.setPort(8080);
tomcat.getConnector(); //"It doesn't look like this does anything;
maybe it we should remove it… "
tomcat.setBaseDir("/foo/bar");
If I'm forced to create a connector, I would at least like the option to
do something normal and expected, such as:
tomcat.getService().addConnector(tomcat.createDefaultConnector());
I _could_ create a default connector manually, but I'd have to be
copying more of the source code (along with "magic" identifiers such as
"HTTP/1.1"). So returning to the first doubt above: why am I using the
`Tomcat` class if it doesn't do the default things for me automatically?
Garret
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org
Re: unable to serve static files with embedded Tomcat
Posted by Rémy Maucherat <re...@apache.org>.
On Fri, Apr 19, 2019 at 7:46 PM Woonsan Ko <wo...@apache.org> wrote:
> On Fri, Apr 19, 2019 at 1:24 PM Garret Wilson <ga...@globalmentor.com>
> wrote:
> >
> > Embedding Tomcat 9 (with OpenJDK 11 on Windows 10) I want to serve
> > static files from `/foo/bar`. From scouring the web (primarily
> > https://stackoverflow.com/a/15235711/421049 ), the documentation, and
> > some books (primarily _Apache Tomcat 7_), I have this:
> >
> > Tomcat tomcat = new Tomcat();
> > tomcat.setPort(8080);
> > tomcat.setBaseDir("/foo");
>
> I found this before from
>
> https://stackoverflow.com/questions/48998387/code-works-with-embedded-apache-tomcat-8-but-not-with-9-whats-changed/49011424
>
> // Note: make sure that tomcat creates the default connector...
> tomcat.getConnector();
>
> Worth trying...
>
That looks correct, and it's easy to spot with the logging (the connector
logs that it's starting if it's there). I removed the magic creation of the
connector because it was causing far more hacks (where the magic connector
had to be removed after its startup). It seemed more normal to have to
explicitly create a connector.
Rémy
>
> Woonsan
>
> > Context ctx = tomcat.addContext("", "/foo/bar");
> > final Wrapper defaultServlet = ctx.createWrapper();
> > defaultServlet.setName("default");
> >
> defaultServlet.setServletClass("org.apache.catalina.servlets.DefaultServlet");
> > defaultServlet.addInitParameter("debug", "1");
> > defaultServlet.addInitParameter("listings", "false");
> > defaultServlet.setLoadOnStartup(1);
> > ctx.addChild(defaultServlet);
> > ctx.addServletMappingDecoded("/", "default");
> > ctx.addWelcomeFile("index.html");
> > tomcat.start();
> > tomcat.getServer().await();
> >
> > Everything looks like it starts up:
> >
> > Apr 19, 2019 2:18:09 PM org.apache.catalina.core.StandardService
> > startInternal
> > INFO: Starting service [Tomcat]
> > Apr 19, 2019 2:18:09 PM org.apache.catalina.core.StandardEngine
> > startInternal
> > INFO: Starting Servlet engine: [Apache Tomcat/9.0.19]
> > Apr 19, 2019 2:18:10 PM
> > org.apache.catalina.util.SessionIdGeneratorBase createSecureRandom
> > WARNING: Creation of SecureRandom instance for session ID
> > generation using [SHA1PRNG] took [281] milliseconds.
> > Apr 19, 2019 2:18:10 PM org.apache.catalina.core.ApplicationContext
> log
> > INFO: default: DefaultServlet.init: input buffer size=2048, output
> > buffer size=2048
> >
> > But connections to http://localhost:8080/ time out. (I'm pretty sure but
> > not positive that I don't have anything blocking this in the firewall.)
> >
> > Is there some simple thing I'm missing to connect things together?
> >
> > Thanks,
> >
> > Garret
> >
> > P.S. The documentation on the web for the details of this is
> > surprisingly sparse.
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> > For additional commands, e-mail: users-help@tomcat.apache.org
> >
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>
>
Re: unable to serve static files with embedded Tomcat
Posted by Woonsan Ko <wo...@apache.org>.
On Fri, Apr 19, 2019 at 1:24 PM Garret Wilson <ga...@globalmentor.com> wrote:
>
> Embedding Tomcat 9 (with OpenJDK 11 on Windows 10) I want to serve
> static files from `/foo/bar`. From scouring the web (primarily
> https://stackoverflow.com/a/15235711/421049 ), the documentation, and
> some books (primarily _Apache Tomcat 7_), I have this:
>
> Tomcat tomcat = new Tomcat();
> tomcat.setPort(8080);
> tomcat.setBaseDir("/foo");
I found this before from
https://stackoverflow.com/questions/48998387/code-works-with-embedded-apache-tomcat-8-but-not-with-9-whats-changed/49011424
// Note: make sure that tomcat creates the default connector...
tomcat.getConnector();
Worth trying...
Woonsan
> Context ctx = tomcat.addContext("", "/foo/bar");
> final Wrapper defaultServlet = ctx.createWrapper();
> defaultServlet.setName("default");
> defaultServlet.setServletClass("org.apache.catalina.servlets.DefaultServlet");
> defaultServlet.addInitParameter("debug", "1");
> defaultServlet.addInitParameter("listings", "false");
> defaultServlet.setLoadOnStartup(1);
> ctx.addChild(defaultServlet);
> ctx.addServletMappingDecoded("/", "default");
> ctx.addWelcomeFile("index.html");
> tomcat.start();
> tomcat.getServer().await();
>
> Everything looks like it starts up:
>
> Apr 19, 2019 2:18:09 PM org.apache.catalina.core.StandardService
> startInternal
> INFO: Starting service [Tomcat]
> Apr 19, 2019 2:18:09 PM org.apache.catalina.core.StandardEngine
> startInternal
> INFO: Starting Servlet engine: [Apache Tomcat/9.0.19]
> Apr 19, 2019 2:18:10 PM
> org.apache.catalina.util.SessionIdGeneratorBase createSecureRandom
> WARNING: Creation of SecureRandom instance for session ID
> generation using [SHA1PRNG] took [281] milliseconds.
> Apr 19, 2019 2:18:10 PM org.apache.catalina.core.ApplicationContext log
> INFO: default: DefaultServlet.init: input buffer size=2048, output
> buffer size=2048
>
> But connections to http://localhost:8080/ time out. (I'm pretty sure but
> not positive that I don't have anything blocking this in the firewall.)
>
> Is there some simple thing I'm missing to connect things together?
>
> Thanks,
>
> Garret
>
> P.S. The documentation on the web for the details of this is
> surprisingly sparse.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org
Re: unable to serve static files with embedded Tomcat
Posted by John Dale <jc...@gmail.com>.
Tomcat doesn't seem to timeout one its own unless my DBCP is being
abused in the code.
I'm thinking firewall .. also, double-check your port configuration(s)
in server.xml (or context.xml if that's the route you're going).
On 4/19/19, Garret Wilson <ga...@globalmentor.com> wrote:
> Embedding Tomcat 9 (with OpenJDK 11 on Windows 10) I want to serve
> static files from `/foo/bar`. From scouring the web (primarily
> https://stackoverflow.com/a/15235711/421049 ), the documentation, and
> some books (primarily _Apache Tomcat 7_), I have this:
>
> Tomcat tomcat = new Tomcat();
> tomcat.setPort(8080);
> tomcat.setBaseDir("/foo");
> Context ctx = tomcat.addContext("", "/foo/bar");
> final Wrapper defaultServlet = ctx.createWrapper();
> defaultServlet.setName("default");
> defaultServlet.setServletClass("org.apache.catalina.servlets.DefaultServlet");
> defaultServlet.addInitParameter("debug", "1");
> defaultServlet.addInitParameter("listings", "false");
> defaultServlet.setLoadOnStartup(1);
> ctx.addChild(defaultServlet);
> ctx.addServletMappingDecoded("/", "default");
> ctx.addWelcomeFile("index.html");
> tomcat.start();
> tomcat.getServer().await();
>
> Everything looks like it starts up:
>
> Apr 19, 2019 2:18:09 PM org.apache.catalina.core.StandardService
> startInternal
> INFO: Starting service [Tomcat]
> Apr 19, 2019 2:18:09 PM org.apache.catalina.core.StandardEngine
> startInternal
> INFO: Starting Servlet engine: [Apache Tomcat/9.0.19]
> Apr 19, 2019 2:18:10 PM
> org.apache.catalina.util.SessionIdGeneratorBase createSecureRandom
> WARNING: Creation of SecureRandom instance for session ID
> generation using [SHA1PRNG] took [281] milliseconds.
> Apr 19, 2019 2:18:10 PM org.apache.catalina.core.ApplicationContext
> log
> INFO: default: DefaultServlet.init: input buffer size=2048, output
> buffer size=2048
>
> But connections to http://localhost:8080/ time out. (I'm pretty sure but
> not positive that I don't have anything blocking this in the firewall.)
>
> Is there some simple thing I'm missing to connect things together?
>
> Thanks,
>
> Garret
>
> P.S. The documentation on the web for the details of this is
> surprisingly sparse.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org