Return-Path: X-Original-To: apmail-tomcat-users-archive@www.apache.org Delivered-To: apmail-tomcat-users-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 547C2115C1 for ; Tue, 1 Jul 2014 16:16:46 +0000 (UTC) Received: (qmail 59957 invoked by uid 500); 1 Jul 2014 16:16:43 -0000 Delivered-To: apmail-tomcat-users-archive@tomcat.apache.org Received: (qmail 59886 invoked by uid 500); 1 Jul 2014 16:16:42 -0000 Mailing-List: contact users-help@tomcat.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: "Tomcat Users List" Delivered-To: mailing list users@tomcat.apache.org Received: (qmail 59875 invoked by uid 99); 1 Jul 2014 16:16:42 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 01 Jul 2014 16:16:42 +0000 X-ASF-Spam-Status: No, hits=-0.7 required=5.0 tests=RCVD_IN_DNSWL_LOW,SPF_PASS X-Spam-Check-By: apache.org Received-SPF: pass (athena.apache.org: domain of john.d.ament@gmail.com designates 209.85.219.53 as permitted sender) Received: from [209.85.219.53] (HELO mail-oa0-f53.google.com) (209.85.219.53) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 01 Jul 2014 16:16:38 +0000 Received: by mail-oa0-f53.google.com with SMTP id l6so10788096oag.40 for ; Tue, 01 Jul 2014 09:16:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type:content-transfer-encoding; bh=tCH3JU5Ekn0rSJ4IXUSIqdY+MhzA4o3wwCahTKEEnzo=; b=Pj0ZEyuW6GuXyRSxdE1UqOvyfHSB4OmYoUG28a7pD/Xya42WtbVwttiek7yWW+JSM1 JoNd38/qgRe2OaC6aRpsLN3nt2nnpHIPgTrkz5hL1xcp4ETDOjzvheCGLC0zc8yowLER lofEwooamu1BTRLovhQEy7r+t93l2RHhj835ejFV4zOi5nOOyYpCOpMv/8Fqz3MLXTTX mHnxJrdRivDEqZzrKxwClvYze5kdYMUoNyrIHAdMTGd7sIYXRIZxaypRnGv+bL4pNbuG exU9R/dhSHfHZ7GaYqKf/SXxB2oaEmN8tK2nrzBbL3Ji2rNgRazfVJiY0m2+jjEmDLX0 rC6w== MIME-Version: 1.0 X-Received: by 10.182.97.97 with SMTP id dz1mr51390279obb.13.1404231377928; Tue, 01 Jul 2014 09:16:17 -0700 (PDT) Received: by 10.182.103.194 with HTTP; Tue, 1 Jul 2014 09:16:17 -0700 (PDT) In-Reply-To: <53B1250F.902@apache.org> References: <99C8B2929B39C24493377AC7A121E21FD9B667B99D@USEA-EXCH8.na.uis.unisys.com> <53B1250F.902@apache.org> Date: Tue, 1 Jul 2014 12:16:17 -0400 Message-ID: Subject: Re: Embedded Tomcat question From: "John D. Ament" To: Tomcat Users List Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-Virus-Checked: Checked by ClamAV on apache.org 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 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(Ap= plicationContext.java:1402) >> >> at org.apache.catalina.core.ApplicationContext.addListener(Appli= cationContext.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 = 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 =3D >>> 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 =3D new Tomcat(); >>> tomcat.setPort(8080); >>> File base =3D new File("target/webapp-runner"); >>> if(!base.exists()) { >>> base.mkdirs(); >>> } >>> Context ctx =3D 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 >>> 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 PROPRIETA= RY MATERIAL and is thus for use only by the intended recipient. If you rece= ived 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