You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Boris Petrov <bo...@profuzdigital.com> on 2017/02/15 15:40:17 UTC

Using Embedded Tomcat with Servlet 3 Annotations

Hello,

We are trying to run an Embedded Tomcat server (version 8.5.11) and have
it process some @WebServlet and @WebListener annotations. We are using
the "addContext" method of the Tomcat instance instead of the
"addWebapp" because we do not want the JSP servlet, the default web.xml
and none of the other things a "webapp" provides. However, after
spending some time reading the code and Googling, I'm reaching the
conclusion that one cannot use "addContext" and have Tomcat use the
annotated classes. Am I right? Why is that, if so? And what do you
suggest we do?

Regards,

Boris Petrov



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


Re: Using Embedded Tomcat with Servlet 3 Annotations

Posted by Boris Petrov <bo...@profuzdigital.com>.
Hi,

Thanks for the quick answer! Actually, we've been doing what you said
all along (however we were doing it before with Servlets and Listeners
described in web.xml and just now tried to run our code with
annotations) but it still doesn't work. We're using JRuby, here's some code:

    tomcat = Tomcat.new
    tomcat.setPort(8081)
    base = java.io.File.new('some-dir')
    root_ctx = tomcat.addContext('/', base.getAbsolutePath)
    root_ctx.addMimeMapping('css', 'text/css')
    root_ctx.setLoader(WebappLoader.new(JRuby.runtime.jruby_class_loader))
    root_ctx.getJarScanner.setScanManifest(false)

    ctx_cfg = ContextConfig.new
    ctx_cfg.setDefaultWebXml(tomcat.noDefaultWebXmlPath)
    root_ctx.addLifecycleListener(ctx_cfg)

    tomcat.start

This is what we've been using. However it doesn't work - Tomcat doesn't
find and load the Listeners and Servlets. Where are we erring?

Another problem that I see with this code is our "manual" adding of a
mime mapping. Is there a "smarter" way to do that with Tomcat's
defaults? We saw Tomcat.DEFAULT_MIME_MAPPINGS but it is private...

Thanks!


On 02/15/2017 05:54 PM, Mark Thomas wrote:
> On 15/02/17 15:40, Boris Petrov wrote:
>> Hello,
>>
>> We are trying to run an Embedded Tomcat server (version 8.5.11) and have
>> it process some @WebServlet and @WebListener annotations. We are using
>> the "addContext" method of the Tomcat instance instead of the
>> "addWebapp" because we do not want the JSP servlet, the default web.xml
>> and none of the other things a "webapp" provides. However, after
>> spending some time reading the code and Googling, I'm reaching the
>> conclusion that one cannot use "addContext" and have Tomcat use the
>> annotated classes. Am I right?
>
> No.
>
>> Why is that, if so?
>
> addContext() is designed for entirely programmatic configuration of
> your web application.
>
>> And what do you suggest we do?
>
> Take a look at the addWebapp() code and adapt it for your needs. The
> short version is you probably need to use addContext() and add a
> ContextConfig instance as a LifecycleListener. It does the annotatioin
> scanning. The bit you don't want is the DefaultWebXmlListener instance.
>
> Mark
>
>
> ---------------------------------------------------------------------
> 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: Using Embedded Tomcat with Servlet 3 Annotations

Posted by Mark Thomas <ma...@apache.org>.
On 15/02/17 15:40, Boris Petrov wrote:
> Hello,
>
> We are trying to run an Embedded Tomcat server (version 8.5.11) and have
> it process some @WebServlet and @WebListener annotations. We are using
> the "addContext" method of the Tomcat instance instead of the
> "addWebapp" because we do not want the JSP servlet, the default web.xml
> and none of the other things a "webapp" provides. However, after
> spending some time reading the code and Googling, I'm reaching the
> conclusion that one cannot use "addContext" and have Tomcat use the
> annotated classes. Am I right?

No.

> Why is that, if so?

addContext() is designed for entirely programmatic configuration of your 
web application.

> And what do you suggest we do?

Take a look at the addWebapp() code and adapt it for your needs. The 
short version is you probably need to use addContext() and add a 
ContextConfig instance as a LifecycleListener. It does the annotatioin 
scanning. The bit you don't want is the DefaultWebXmlListener instance.

Mark


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