tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Christopher Schultz <ch...@christopherschultz.net>
Subject Re: Unable to establish WebSocket connection on Tomcat Embed (9.0.24)
Date Mon, 04 Nov 2019 18:48:25 GMT
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256

Alex,

On 11/4/19 10:09, Alexander Stöcker wrote:
> On Mon, Nov 4, 2019 at 2:55 PM Mark Thomas <markt@apache.org>
> wrote:
>> 
>> On 04/11/2019 12:59, Alexander Stöcker wrote:
>>> My code is basically just:
>>> 
>>> Tomcat tomcat = new Tomcat(); tomcat.getConnector(); 
>>> tomcat.setPort(8080);
>>> 
>>> Context context = tomcat.addContext("/context", new 
>>> File(".").getAbsolutePath());
>>> 
>>> // servlet Tomcat.addServlet(context, "myServlet", new
>>> MyServlet()); context.addServletMappingDecoded("/hello",
>>> "myServlet");
>>> 
>>> // websocket 
>>> context.addApplicationListener(MyWSContextListener.class.getName());
>>>
>>>
>>> 
// run
>>> tomcat.start(); tomcat.getServer().await();
>>> 
>>> 
>>> The WsContextListener is adding an Endpoint when
>>> contextInitialized() is called. Setting a Breakpoint shows that
>>> the code is actually executed. The Servlet is working but when
>>> I'm trying to connect to the endpoint
>>> ("ws://localhost:8080/context/endpoint") I get a 404. On Fine 
>>> debug output I can see, that this exception is thrown, whenever
>>> a connection is requested:
>> 
>> You need to configure the default servlet. Otherwise the mapper
>> looks at the URI, sees that it doesn't match any known servlets
>> and rejects the request with a 404 before the WebSocket filter
>> has a chance to handle the request.
>> 
>> Mark
>> 
>> ---------------------------------------------------------------------
>>
>> 
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
>> For additional commands, e-mail: users-help@tomcat.apache.org
>> 
> 
> Indeed, that solved the Problem. I would have never been able to
> solve that just from documentation or examples... After some
> googling on "default servlet" I found this SO thread which gave me
> the solution:
> 
> https://stackoverflow.com/questions/6349472/embedded-tomcat-not-servin
g-static-content
>
>  TL;DR: You could either write:
> 
> Wrapper defaultServlet = context.createWrapper(); 
> defaultServlet.setName("default"); 
> defaultServlet.setServletClass("org.apache.catalina.servlets.DefaultSe
rvlet");
>
> 
defaultServlet.setLoadOnStartup(1);
> context.addChild(defaultServlet); 
> context.addServletMappingDecoded("/", "default");
> 
> or  just
> 
> Tomcat.initWebappDefaults(context);

It seems like Tomcat.initDefaultServlet(context) would be a good
addition to the API. That would give you only the default servlet but
not also e.g. JSPServlet if you didn't need/want it.

- -chris
-----BEGIN PGP SIGNATURE-----
Comment: Using GnuPG with Thunderbird - https://www.enigmail.net/

iQIzBAEBCAAdFiEEMmKgYcQvxMe7tcJcHPApP6U8pFgFAl3AcnkACgkQHPApP6U8
pFhrKw/+NoavjCORgEh69v5s5SN3UIh/ksHkseACOU67TdzmwVhn2RK2UfM/fiMn
c62Eqh2wD99rGWyM9vgBUTeZ35ANMdczrnEfCCBw0XUnSdZ/OxLpdhIhtQ1KaefD
fAeNicNIs5g5X5e8R7lRCE/teu881jyUxWcNWo1gcHz+S5WPyK4VCG2O3TGful4Z
QvpDYvg89eiBx5Gh8qhEk59PglXUbwLZ7UCUG/kl6a+OdfhXQQFErwwphT0SsJTp
GdZxyU2JJpE62jKGPvOpdbFBwhfbHvqRQo+TZfsJf2Ur7hdUj3bEwCE9Ue+EgSIC
zKkxKxB0gu7ZYvQX+0nt2fucfWMBz+BRfFasqB8gYh9VBNkwD2WxLZnFosgG5wUJ
SSZrMkCjmheUgVz7bdpkjeV4IAlWGNGitq2nYb2xfGlpWn4ht3B9+23ZyOXCTFYB
gyL+OFO/LHv5CJkgaRVpmXenXYQtW8y3MFb/eqzRy9NpZtHmjtIu8at1O4BTIcDa
T5nIYUuuumbKFrTcTHeCryn9bAxQ7xnqpxCqQqo9oRmXzRctD+ISggmSE5eVUVur
uhWXzhdTaixU+K4KovYzY0tgCwcTkIFLN2NwTklp1Odb6JjSTfq9gK/RWGQu4/PQ
y5Cz+h9o0iTwXJP/ZHeqvL53WbThELkjbiO6ku/k9U9NMnm8UlQ=
=em5M
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Mime
View raw message