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.am...@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.caldar...@unisys.com> wrote: >>>> From: John D. Ament [mailto:john.d.am...@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-unsubscr...@tomcat.apache.org >>> For additional commands, e-mail: users-h...@tomcat.apache.org >>> > > --------------------------------------------------------------------- > To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org > For additional commands, e-mail: users-h...@tomcat.apache.org > --------------------------------------------------------------------- To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org For additional commands, e-mail: users-h...@tomcat.apache.org