tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Mark Thomas <ma...@apache.org>
Subject Re: Embedded Tomcat question
Date Tue, 01 Jul 2014 16:37:04 GMT
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 <markt@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 <john.d.ament@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
>>>> <Chuck.Caldarale@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


Mime
View raw message