You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by "John D. Ament" <jo...@gmail.com> on 2014/06/29 16:17:17 UTC

Embedded Tomcat question

Hi,

Playing around a bit with embedded tomcat.  It looks like there are
APIs to add all of the tomcat specific listeners, however how would I
add a ServletRequestListener?

John

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


Re: Embedded Tomcat question

Posted by Mark Thomas <ma...@apache.org>.
On 10/07/2014 03:13, John D. Ament wrote:
> Well, thanks for the helpful advice thus far..
> 
> So, now that I've added a listener properly, I get this:
> 
> java.lang.UnsupportedOperationException: Section 4.4 of the Servlet
> 3.0 specification does not permit this method to be called from a
> ServletContextListener that was not defined in web.xml, a
> web-fragment.xml file nor annotated with @WebListener
> 
> at org.apache.catalina.core.StandardContext$NoPluggabilityServletContext.addListener(StandardContext.java:7019)
> 
> When I try adding my listener...

Hmm. The method names in StandardContext aren't helping here. We can't
change them (it would break a public API) but we could probably improve
the Javadoc. Patches welcome...

You haven't provided the code you used to add CdiServletContextListener
to the StandardContext so I am having to do some guessing here.

I think you added an instance of that class via
StandardContext.addApplicationLifecycleListener(). If that is the case,
try adding the listener via the class name using
StandardContext.addApplicationListener().

The first method is intended to be used by the pluggability API and the
exception you are running into is triggered when a listener added via
the pluggability API itself tries to use the plubggability API to add a
listener of its own (section 4.4 of the Servlet spec does not permit this).

If you use the second method it is equivalent to adding the listener to
web.xml where it is able to then able to use the pluggability API to add
further listeners.

Of course, all of that information isn't going to help if my guess about
you using addApplicationLifecycleListener() is wrong. If I am wrong,
please provide the code you use to create the context and add the
listener and we can take another look.

Mark


> 
> @WebListener
> public class CdiServletContextListener implements ServletContextListener
> {
>     @Override
>     public void contextInitialized(ServletContextEvent servletContextEvent)
>     {
>         servletContextEvent.getServletContext().addListener(new
> CdiCtrlListener());
>     }
> 
> I even tried rigging my class to have WebListener on it.. until I
> noticed the method just threw an exception... no questions asked. :-)
> 
> On Tue, Jul 1, 2014 at 12:37 PM, Mark Thomas <ma...@apache.org> wrote:
>> On 01/07/2014 17:16, John D. Ament wrote:
>>> I looked for the source code, at least on github, there's no tag for
>>> 7.0.55 defined (see [1])
>>
>> Given you are using 7.0.54, why would you look for a tag for 7.0.55?
>>
>>> How do I get access to a StandardContext?  CAn I cast the Context object?
>>
>> Yes. It would have taken less time to try that than it did to type the
>> question.
>>
>> Mark
>>
>>
>>>
>>> [1]: https://github.com/apache/tomcat
>>>
>>> On Mon, Jun 30, 2014 at 4:51 AM, Mark Thomas <ma...@apache.org> wrote:
>>>> On 30/06/2014 01:27, John D. Ament wrote:
>>>>> I spoke a little too quickly, ServletContext is avilable from ctx
>>>>> (just didn't search hard enough).  Adding the request listener here
>>>>> though results in:
>>>>>
>>>>> java.lang.NullPointerException
>>>>>
>>>>>         at org.apache.catalina.core.ApplicationContext.createListener(ApplicationContext.java:1402)
>>>>>
>>>>>         at org.apache.catalina.core.ApplicationContext.addListener(ApplicationContext.java:1307)
>>>>>
>>>>>         at org.apache.catalina.core.ApplicationContextFacade.addListener(ApplicationContextFacade.java:636)
>>>>
>>>> A quick look in the source code will tell you why you are getting an NPE
>>>> here.
>>>>
>>>> You can't add a ServletRequestListener directly. There are a couple of
>>>> ways to do this. Probably the simplest is to create a
>>>> ServletContextListener, add that directly to Tomcat's StandardContext
>>>> with addApplicationLifecycleListener() and then from that
>>>> ServletContextListener add and request and/or session listeners from the
>>>> contextInitialized() event.
>>>>
>>>> You could also do this with a ServletContainerInitializer.
>>>>
>>>> Mark
>>>>
>>>>
>>>>>
>>>>> On Sun, Jun 29, 2014 at 8:21 PM, John D. Ament <jo...@gmail.com> wrote:
>>>>>> Hi,
>>>>>>
>>>>>> I'm on Tomcat 7.0.54, though I would hope your APIs aren't volatile
>>>>>> enough that your internals should matter..
>>>>>>
>>>>>> I tried this:
>>>>>>
>>>>>> Wrapper wrapper =
>>>>>> Tomcat.addServlet(ctx,"Greeter",GreeterServlet.class.getName());
>>>>>> wrapper.addMapping("/*");
>>>>>> wrapper.getServlet().getServletConfig().getServletContext().addListener(MyListener.class);
>>>>>>
>>>>>> This results in a NPE.  This makes me think I don't have access to the
>>>>>> context yet.  I would actually think it's somewhere in this chain:
>>>>>>
>>>>>>         tomcat = new Tomcat();
>>>>>>         tomcat.setPort(8080);
>>>>>>         File base = new File("target/webapp-runner");
>>>>>>         if(!base.exists()) {
>>>>>>             base.mkdirs();
>>>>>>         }
>>>>>>         Context ctx = tomcat.addContext("/",base.getAbsolutePath());
>>>>>>
>>>>>> however, it doesn't look like Context here is a ServletContext.
>>>>>>
>>>>>>
>>>>>> On Sun, Jun 29, 2014 at 6:30 PM, Caldarale, Charles R
>>>>>> <Ch...@unisys.com> wrote:
>>>>>>>> From: John D. Ament [mailto:john.d.ament@gmail.com]
>>>>>>>> Subject: Embedded Tomcat question
>>>>>>>
>>>>>>>> Playing around a bit with embedded tomcat.  It looks like there are
>>>>>>>> APIs to add all of the tomcat specific listeners, however how would I
>>>>>>>> add a ServletRequestListener?
>>>>>>>
>>>>>>> Look in the servlet spec for the version of Tomcat you're using (which you didn't mention), and the JavaDoc for ServletContext (e.g., addListener).
>>>>>>>
>>>>>>>  - Chuck
>>>>>>>
>>>>>>>
>>>>>>> THIS COMMUNICATION MAY CONTAIN CONFIDENTIAL AND/OR OTHERWISE PROPRIETARY MATERIAL and is thus for use only by the intended recipient. If you received this in error, please contact the sender and delete the e-mail and its attachments from all computers.
>>>>>>>
>>>>>>>
>>>>>>> ---------------------------------------------------------------------
>>>>>>> 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
>>>>>
>>>>
>>>>
>>>> ---------------------------------------------------------------------
>>>> 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
>>>
>>
>>
>> ---------------------------------------------------------------------
>> 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
> 


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


Re: Embedded Tomcat question

Posted by "John D. Ament" <jo...@gmail.com>.
Well, thanks for the helpful advice thus far..

So, now that I've added a listener properly, I get this:

java.lang.UnsupportedOperationException: Section 4.4 of the Servlet
3.0 specification does not permit this method to be called from a
ServletContextListener that was not defined in web.xml, a
web-fragment.xml file nor annotated with @WebListener

at org.apache.catalina.core.StandardContext$NoPluggabilityServletContext.addListener(StandardContext.java:7019)

When I try adding my listener...

@WebListener
public class CdiServletContextListener implements ServletContextListener
{
    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent)
    {
        servletContextEvent.getServletContext().addListener(new
CdiCtrlListener());
    }

I even tried rigging my class to have WebListener on it.. until I
noticed the method just threw an exception... no questions asked. :-)

On Tue, Jul 1, 2014 at 12:37 PM, Mark Thomas <ma...@apache.org> wrote:
> On 01/07/2014 17:16, John D. Ament wrote:
>> I looked for the source code, at least on github, there's no tag for
>> 7.0.55 defined (see [1])
>
> Given you are using 7.0.54, why would you look for a tag for 7.0.55?
>
>> How do I get access to a StandardContext?  CAn I cast the Context object?
>
> Yes. It would have taken less time to try that than it did to type the
> question.
>
> Mark
>
>
>>
>> [1]: https://github.com/apache/tomcat
>>
>> On Mon, Jun 30, 2014 at 4:51 AM, Mark Thomas <ma...@apache.org> wrote:
>>> On 30/06/2014 01:27, John D. Ament wrote:
>>>> I spoke a little too quickly, ServletContext is avilable from ctx
>>>> (just didn't search hard enough).  Adding the request listener here
>>>> though results in:
>>>>
>>>> java.lang.NullPointerException
>>>>
>>>>         at org.apache.catalina.core.ApplicationContext.createListener(ApplicationContext.java:1402)
>>>>
>>>>         at org.apache.catalina.core.ApplicationContext.addListener(ApplicationContext.java:1307)
>>>>
>>>>         at org.apache.catalina.core.ApplicationContextFacade.addListener(ApplicationContextFacade.java:636)
>>>
>>> A quick look in the source code will tell you why you are getting an NPE
>>> here.
>>>
>>> You can't add a ServletRequestListener directly. There are a couple of
>>> ways to do this. Probably the simplest is to create a
>>> ServletContextListener, add that directly to Tomcat's StandardContext
>>> with addApplicationLifecycleListener() and then from that
>>> ServletContextListener add and request and/or session listeners from the
>>> contextInitialized() event.
>>>
>>> You could also do this with a ServletContainerInitializer.
>>>
>>> Mark
>>>
>>>
>>>>
>>>> On Sun, Jun 29, 2014 at 8:21 PM, John D. Ament <jo...@gmail.com> wrote:
>>>>> Hi,
>>>>>
>>>>> I'm on Tomcat 7.0.54, though I would hope your APIs aren't volatile
>>>>> enough that your internals should matter..
>>>>>
>>>>> I tried this:
>>>>>
>>>>> Wrapper wrapper =
>>>>> Tomcat.addServlet(ctx,"Greeter",GreeterServlet.class.getName());
>>>>> wrapper.addMapping("/*");
>>>>> wrapper.getServlet().getServletConfig().getServletContext().addListener(MyListener.class);
>>>>>
>>>>> This results in a NPE.  This makes me think I don't have access to the
>>>>> context yet.  I would actually think it's somewhere in this chain:
>>>>>
>>>>>         tomcat = new Tomcat();
>>>>>         tomcat.setPort(8080);
>>>>>         File base = new File("target/webapp-runner");
>>>>>         if(!base.exists()) {
>>>>>             base.mkdirs();
>>>>>         }
>>>>>         Context ctx = tomcat.addContext("/",base.getAbsolutePath());
>>>>>
>>>>> however, it doesn't look like Context here is a ServletContext.
>>>>>
>>>>>
>>>>> On Sun, Jun 29, 2014 at 6:30 PM, Caldarale, Charles R
>>>>> <Ch...@unisys.com> wrote:
>>>>>>> From: John D. Ament [mailto:john.d.ament@gmail.com]
>>>>>>> Subject: Embedded Tomcat question
>>>>>>
>>>>>>> Playing around a bit with embedded tomcat.  It looks like there are
>>>>>>> APIs to add all of the tomcat specific listeners, however how would I
>>>>>>> add a ServletRequestListener?
>>>>>>
>>>>>> Look in the servlet spec for the version of Tomcat you're using (which you didn't mention), and the JavaDoc for ServletContext (e.g., addListener).
>>>>>>
>>>>>>  - Chuck
>>>>>>
>>>>>>
>>>>>> THIS COMMUNICATION MAY CONTAIN CONFIDENTIAL AND/OR OTHERWISE PROPRIETARY MATERIAL and is thus for use only by the intended recipient. If you received this in error, please contact the sender and delete the e-mail and its attachments from all computers.
>>>>>>
>>>>>>
>>>>>> ---------------------------------------------------------------------
>>>>>> 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
>>>>
>>>
>>>
>>> ---------------------------------------------------------------------
>>> 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
>>
>
>
> ---------------------------------------------------------------------
> 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: Embedded Tomcat question

Posted by Mark Thomas <ma...@apache.org>.
On 01/07/2014 17:16, John D. Ament wrote:
> I looked for the source code, at least on github, there's no tag for
> 7.0.55 defined (see [1])

Given you are using 7.0.54, why would you look for a tag for 7.0.55?

> How do I get access to a StandardContext?  CAn I cast the Context object?

Yes. It would have taken less time to try that than it did to type the
question.

Mark


> 
> [1]: https://github.com/apache/tomcat
> 
> On Mon, Jun 30, 2014 at 4:51 AM, Mark Thomas <ma...@apache.org> wrote:
>> On 30/06/2014 01:27, John D. Ament wrote:
>>> I spoke a little too quickly, ServletContext is avilable from ctx
>>> (just didn't search hard enough).  Adding the request listener here
>>> though results in:
>>>
>>> java.lang.NullPointerException
>>>
>>>         at org.apache.catalina.core.ApplicationContext.createListener(ApplicationContext.java:1402)
>>>
>>>         at org.apache.catalina.core.ApplicationContext.addListener(ApplicationContext.java:1307)
>>>
>>>         at org.apache.catalina.core.ApplicationContextFacade.addListener(ApplicationContextFacade.java:636)
>>
>> A quick look in the source code will tell you why you are getting an NPE
>> here.
>>
>> You can't add a ServletRequestListener directly. There are a couple of
>> ways to do this. Probably the simplest is to create a
>> ServletContextListener, add that directly to Tomcat's StandardContext
>> with addApplicationLifecycleListener() and then from that
>> ServletContextListener add and request and/or session listeners from the
>> contextInitialized() event.
>>
>> You could also do this with a ServletContainerInitializer.
>>
>> Mark
>>
>>
>>>
>>> On Sun, Jun 29, 2014 at 8:21 PM, John D. Ament <jo...@gmail.com> wrote:
>>>> Hi,
>>>>
>>>> I'm on Tomcat 7.0.54, though I would hope your APIs aren't volatile
>>>> enough that your internals should matter..
>>>>
>>>> I tried this:
>>>>
>>>> Wrapper wrapper =
>>>> Tomcat.addServlet(ctx,"Greeter",GreeterServlet.class.getName());
>>>> wrapper.addMapping("/*");
>>>> wrapper.getServlet().getServletConfig().getServletContext().addListener(MyListener.class);
>>>>
>>>> This results in a NPE.  This makes me think I don't have access to the
>>>> context yet.  I would actually think it's somewhere in this chain:
>>>>
>>>>         tomcat = new Tomcat();
>>>>         tomcat.setPort(8080);
>>>>         File base = new File("target/webapp-runner");
>>>>         if(!base.exists()) {
>>>>             base.mkdirs();
>>>>         }
>>>>         Context ctx = tomcat.addContext("/",base.getAbsolutePath());
>>>>
>>>> however, it doesn't look like Context here is a ServletContext.
>>>>
>>>>
>>>> On Sun, Jun 29, 2014 at 6:30 PM, Caldarale, Charles R
>>>> <Ch...@unisys.com> wrote:
>>>>>> From: John D. Ament [mailto:john.d.ament@gmail.com]
>>>>>> Subject: Embedded Tomcat question
>>>>>
>>>>>> Playing around a bit with embedded tomcat.  It looks like there are
>>>>>> APIs to add all of the tomcat specific listeners, however how would I
>>>>>> add a ServletRequestListener?
>>>>>
>>>>> Look in the servlet spec for the version of Tomcat you're using (which you didn't mention), and the JavaDoc for ServletContext (e.g., addListener).
>>>>>
>>>>>  - Chuck
>>>>>
>>>>>
>>>>> THIS COMMUNICATION MAY CONTAIN CONFIDENTIAL AND/OR OTHERWISE PROPRIETARY MATERIAL and is thus for use only by the intended recipient. If you received this in error, please contact the sender and delete the e-mail and its attachments from all computers.
>>>>>
>>>>>
>>>>> ---------------------------------------------------------------------
>>>>> 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
>>>
>>
>>
>> ---------------------------------------------------------------------
>> 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
> 


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


Re: Embedded Tomcat question

Posted by "John D. Ament" <jo...@gmail.com>.
I looked for the source code, at least on github, there's no tag for
7.0.55 defined (see [1])

How do I get access to a StandardContext?  CAn I cast the Context object?

[1]: https://github.com/apache/tomcat

On Mon, Jun 30, 2014 at 4:51 AM, Mark Thomas <ma...@apache.org> wrote:
> On 30/06/2014 01:27, John D. Ament wrote:
>> I spoke a little too quickly, ServletContext is avilable from ctx
>> (just didn't search hard enough).  Adding the request listener here
>> though results in:
>>
>> java.lang.NullPointerException
>>
>>         at org.apache.catalina.core.ApplicationContext.createListener(ApplicationContext.java:1402)
>>
>>         at org.apache.catalina.core.ApplicationContext.addListener(ApplicationContext.java:1307)
>>
>>         at org.apache.catalina.core.ApplicationContextFacade.addListener(ApplicationContextFacade.java:636)
>
> A quick look in the source code will tell you why you are getting an NPE
> here.
>
> You can't add a ServletRequestListener directly. There are a couple of
> ways to do this. Probably the simplest is to create a
> ServletContextListener, add that directly to Tomcat's StandardContext
> with addApplicationLifecycleListener() and then from that
> ServletContextListener add and request and/or session listeners from the
> contextInitialized() event.
>
> You could also do this with a ServletContainerInitializer.
>
> Mark
>
>
>>
>> On Sun, Jun 29, 2014 at 8:21 PM, John D. Ament <jo...@gmail.com> wrote:
>>> Hi,
>>>
>>> I'm on Tomcat 7.0.54, though I would hope your APIs aren't volatile
>>> enough that your internals should matter..
>>>
>>> I tried this:
>>>
>>> Wrapper wrapper =
>>> Tomcat.addServlet(ctx,"Greeter",GreeterServlet.class.getName());
>>> wrapper.addMapping("/*");
>>> wrapper.getServlet().getServletConfig().getServletContext().addListener(MyListener.class);
>>>
>>> This results in a NPE.  This makes me think I don't have access to the
>>> context yet.  I would actually think it's somewhere in this chain:
>>>
>>>         tomcat = new Tomcat();
>>>         tomcat.setPort(8080);
>>>         File base = new File("target/webapp-runner");
>>>         if(!base.exists()) {
>>>             base.mkdirs();
>>>         }
>>>         Context ctx = tomcat.addContext("/",base.getAbsolutePath());
>>>
>>> however, it doesn't look like Context here is a ServletContext.
>>>
>>>
>>> On Sun, Jun 29, 2014 at 6:30 PM, Caldarale, Charles R
>>> <Ch...@unisys.com> wrote:
>>>>> From: John D. Ament [mailto:john.d.ament@gmail.com]
>>>>> Subject: Embedded Tomcat question
>>>>
>>>>> Playing around a bit with embedded tomcat.  It looks like there are
>>>>> APIs to add all of the tomcat specific listeners, however how would I
>>>>> add a ServletRequestListener?
>>>>
>>>> Look in the servlet spec for the version of Tomcat you're using (which you didn't mention), and the JavaDoc for ServletContext (e.g., addListener).
>>>>
>>>>  - Chuck
>>>>
>>>>
>>>> THIS COMMUNICATION MAY CONTAIN CONFIDENTIAL AND/OR OTHERWISE PROPRIETARY MATERIAL and is thus for use only by the intended recipient. If you received this in error, please contact the sender and delete the e-mail and its attachments from all computers.
>>>>
>>>>
>>>> ---------------------------------------------------------------------
>>>> 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
>>
>
>
> ---------------------------------------------------------------------
> 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: Embedded Tomcat question

Posted by Mark Thomas <ma...@apache.org>.
On 30/06/2014 01:27, John D. Ament wrote:
> I spoke a little too quickly, ServletContext is avilable from ctx
> (just didn't search hard enough).  Adding the request listener here
> though results in:
> 
> java.lang.NullPointerException
> 
>         at org.apache.catalina.core.ApplicationContext.createListener(ApplicationContext.java:1402)
> 
>         at org.apache.catalina.core.ApplicationContext.addListener(ApplicationContext.java:1307)
> 
>         at org.apache.catalina.core.ApplicationContextFacade.addListener(ApplicationContextFacade.java:636)

A quick look in the source code will tell you why you are getting an NPE
here.

You can't add a ServletRequestListener directly. There are a couple of
ways to do this. Probably the simplest is to create a
ServletContextListener, add that directly to Tomcat's StandardContext
with addApplicationLifecycleListener() and then from that
ServletContextListener add and request and/or session listeners from the
contextInitialized() event.

You could also do this with a ServletContainerInitializer.

Mark


> 
> On Sun, Jun 29, 2014 at 8:21 PM, John D. Ament <jo...@gmail.com> wrote:
>> Hi,
>>
>> I'm on Tomcat 7.0.54, though I would hope your APIs aren't volatile
>> enough that your internals should matter..
>>
>> I tried this:
>>
>> Wrapper wrapper =
>> Tomcat.addServlet(ctx,"Greeter",GreeterServlet.class.getName());
>> wrapper.addMapping("/*");
>> wrapper.getServlet().getServletConfig().getServletContext().addListener(MyListener.class);
>>
>> This results in a NPE.  This makes me think I don't have access to the
>> context yet.  I would actually think it's somewhere in this chain:
>>
>>         tomcat = new Tomcat();
>>         tomcat.setPort(8080);
>>         File base = new File("target/webapp-runner");
>>         if(!base.exists()) {
>>             base.mkdirs();
>>         }
>>         Context ctx = tomcat.addContext("/",base.getAbsolutePath());
>>
>> however, it doesn't look like Context here is a ServletContext.
>>
>>
>> On Sun, Jun 29, 2014 at 6:30 PM, Caldarale, Charles R
>> <Ch...@unisys.com> wrote:
>>>> From: John D. Ament [mailto:john.d.ament@gmail.com]
>>>> Subject: Embedded Tomcat question
>>>
>>>> Playing around a bit with embedded tomcat.  It looks like there are
>>>> APIs to add all of the tomcat specific listeners, however how would I
>>>> add a ServletRequestListener?
>>>
>>> Look in the servlet spec for the version of Tomcat you're using (which you didn't mention), and the JavaDoc for ServletContext (e.g., addListener).
>>>
>>>  - Chuck
>>>
>>>
>>> THIS COMMUNICATION MAY CONTAIN CONFIDENTIAL AND/OR OTHERWISE PROPRIETARY MATERIAL and is thus for use only by the intended recipient. If you received this in error, please contact the sender and delete the e-mail and its attachments from all computers.
>>>
>>>
>>> ---------------------------------------------------------------------
>>> 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
> 


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


Re: Embedded Tomcat question

Posted by "John D. Ament" <jo...@gmail.com>.
I spoke a little too quickly, ServletContext is avilable from ctx
(just didn't search hard enough).  Adding the request listener here
though results in:

java.lang.NullPointerException

        at org.apache.catalina.core.ApplicationContext.createListener(ApplicationContext.java:1402)

        at org.apache.catalina.core.ApplicationContext.addListener(ApplicationContext.java:1307)

        at org.apache.catalina.core.ApplicationContextFacade.addListener(ApplicationContextFacade.java:636)

On Sun, Jun 29, 2014 at 8:21 PM, John D. Ament <jo...@gmail.com> wrote:
> Hi,
>
> I'm on Tomcat 7.0.54, though I would hope your APIs aren't volatile
> enough that your internals should matter..
>
> I tried this:
>
> Wrapper wrapper =
> Tomcat.addServlet(ctx,"Greeter",GreeterServlet.class.getName());
> wrapper.addMapping("/*");
> wrapper.getServlet().getServletConfig().getServletContext().addListener(MyListener.class);
>
> This results in a NPE.  This makes me think I don't have access to the
> context yet.  I would actually think it's somewhere in this chain:
>
>         tomcat = new Tomcat();
>         tomcat.setPort(8080);
>         File base = new File("target/webapp-runner");
>         if(!base.exists()) {
>             base.mkdirs();
>         }
>         Context ctx = tomcat.addContext("/",base.getAbsolutePath());
>
> however, it doesn't look like Context here is a ServletContext.
>
>
> On Sun, Jun 29, 2014 at 6:30 PM, Caldarale, Charles R
> <Ch...@unisys.com> wrote:
>>> From: John D. Ament [mailto:john.d.ament@gmail.com]
>>> Subject: Embedded Tomcat question
>>
>>> Playing around a bit with embedded tomcat.  It looks like there are
>>> APIs to add all of the tomcat specific listeners, however how would I
>>> add a ServletRequestListener?
>>
>> Look in the servlet spec for the version of Tomcat you're using (which you didn't mention), and the JavaDoc for ServletContext (e.g., addListener).
>>
>>  - Chuck
>>
>>
>> THIS COMMUNICATION MAY CONTAIN CONFIDENTIAL AND/OR OTHERWISE PROPRIETARY MATERIAL and is thus for use only by the intended recipient. If you received this in error, please contact the sender and delete the e-mail and its attachments from all computers.
>>
>>
>> ---------------------------------------------------------------------
>> 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: Embedded Tomcat question

Posted by "John D. Ament" <jo...@gmail.com>.
Hi,

I'm on Tomcat 7.0.54, though I would hope your APIs aren't volatile
enough that your internals should matter..

I tried this:

Wrapper wrapper =
Tomcat.addServlet(ctx,"Greeter",GreeterServlet.class.getName());
wrapper.addMapping("/*");
wrapper.getServlet().getServletConfig().getServletContext().addListener(MyListener.class);

This results in a NPE.  This makes me think I don't have access to the
context yet.  I would actually think it's somewhere in this chain:

        tomcat = new Tomcat();
        tomcat.setPort(8080);
        File base = new File("target/webapp-runner");
        if(!base.exists()) {
            base.mkdirs();
        }
        Context ctx = tomcat.addContext("/",base.getAbsolutePath());

however, it doesn't look like Context here is a ServletContext.


On Sun, Jun 29, 2014 at 6:30 PM, Caldarale, Charles R
<Ch...@unisys.com> wrote:
>> From: John D. Ament [mailto:john.d.ament@gmail.com]
>> Subject: Embedded Tomcat question
>
>> Playing around a bit with embedded tomcat.  It looks like there are
>> APIs to add all of the tomcat specific listeners, however how would I
>> add a ServletRequestListener?
>
> Look in the servlet spec for the version of Tomcat you're using (which you didn't mention), and the JavaDoc for ServletContext (e.g., addListener).
>
>  - Chuck
>
>
> THIS COMMUNICATION MAY CONTAIN CONFIDENTIAL AND/OR OTHERWISE PROPRIETARY MATERIAL and is thus for use only by the intended recipient. If you received this in error, please contact the sender and delete the e-mail and its attachments from all computers.
>
>
> ---------------------------------------------------------------------
> 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: Embedded Tomcat question

Posted by "Caldarale, Charles R" <Ch...@unisys.com>.
> From: John D. Ament [mailto:john.d.ament@gmail.com] 
> Subject: Embedded Tomcat question

> Playing around a bit with embedded tomcat.  It looks like there are
> APIs to add all of the tomcat specific listeners, however how would I
> add a ServletRequestListener?

Look in the servlet spec for the version of Tomcat you're using (which you didn't mention), and the JavaDoc for ServletContext (e.g., addListener).

 - Chuck


THIS COMMUNICATION MAY CONTAIN CONFIDENTIAL AND/OR OTHERWISE PROPRIETARY MATERIAL and is thus for use only by the intended recipient. If you received this in error, please contact the sender and delete the e-mail and its attachments from all computers.


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