Bug 53903 - Embedded Tomcat7 does not process Servlet API3.0 Annotations (WebListener)
Embedded Tomcat7 does not process Servlet API3.0 Annotations (WebListener)
Status: RESOLVED INVALID
Product: Tomcat 7
Classification: Unclassified
Component: Catalina
7.0.29
PC All
: P2 normal (vote)
: ---
Assigned To: Tomcat Developers Mailing List
:
Depends on:
Blocks:
  Show dependency tree
 
Reported: 2012-09-19 14:38 UTC by Manuel Hartl
Modified: 2012-09-27 18:45 UTC (History)
0 users



Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Manuel Hartl 2012-09-19 14:38:29 UTC
When starting an embeeded tomcat with an webapp with
- web.xml with servlet api3 version
- a ServletContextListener implementation with @WebListener Annotation

then the ServletContextlistener is ignored (it wont be registered).

The same Listener works if added manually to web.xml with a <listener>-Tag
Comment 1 Mark Thomas 2012-09-21 07:46:28 UTC
Please provide a test case to reproduce this. Make sure you are using addWebapp() and not addContext()
Comment 2 Manuel Hartl 2012-09-21 08:35:29 UTC
The code snippet for starting:

		Tomcat tomcat = new Tomcat();
		tomcat.setPort(9000);
		tomcat.setBaseDir(".");
		tomcat.enableNaming();

		
		Context ctx = tomcat.addWebapp("/", docBase.getAbsolutePath());
		ctx.setConfigFile(new File("/META-INF/context.xml").toURI().toURL());
		tomcat.start();
		tomcat.getServer().await();

the web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
	version="3.0" metadata-complete="false">
	<display-name>ssms configuration app</display-name>
 
	<!-- Change to "Production" when you are ready to deploy -->
	<context-param>
		<param-name>javax.faces.DEFAULT_SUFFIX</param-name>
		<param-value>.xhtml</param-value>
	</context-param>
	<context-param>
		<param-name>javax.faces.PROJECT_STAGE</param-name>
		<param-value>Development</param-value>
	</context-param>
	
	<context-param>
		<param-name>org.richfaces.skin</param-name>
		<param-value>classic</param-value>
	</context-param>
	
	<context-param>
    	<param-name>javax.faces.FACELETS_SKIP_COMMENTS</param-name>
    	<param-value>true</param-value>
	</context-param>

	<context-param>
		<param-name>createTempFiles</param-name>
		<param-value>false</param-value>
	</context-param>
	
	<context-param>
		<param-name>maxRequestSize</param-name>
		<param-value>1000000</param-value>
	</context-param>
	
	<context-param>
		<param-name>javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL</param-name>
		<param-value>true</param-value>
	</context-param>
 
	<!-- Welcome page -->
	<welcome-file-list>
		<welcome-file>resources/index.xhtml</welcome-file>
	</welcome-file-list>
 
	<!-- JSF mapping -->
	<servlet>
		<servlet-name>Faces Servlet</servlet-name>
		<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
		<load-on-startup>1</load-on-startup>
	</servlet>
 
	<!-- Map these files with JSF -->
	<servlet-mapping>
		<servlet-name>Faces Servlet</servlet-name>
		<url-pattern>*.xhtml</url-pattern>
	</servlet-mapping>
	
	<error-page> 
	    <exception-type>java.lang.Throwable</exception-type> 
	    <location>/resources/error.xhtml</location> 
	</error-page>
	
	<error-page> 
	    <error-code>500</error-code> 
	    <location>/resources/error.xhtml</location> 
	</error-page>
	
	<error-page> 
	    <error-code>503</error-code> 
	    <location>/resources/error.xhtml</location> 
	</error-page>
	<listener>
		<listener-class>org.jboss.weld.environment.servlet.Listener</listener-class>
	</listener>
	<resource-env-ref>
		<resource-env-ref-name>BeanManager</resource-env-ref-name>
		<resource-env-ref-type>javax.enterprise.inject.spi.BeanManager</resource-env-ref-type>
	</resource-env-ref>	
</web-app>

An example ContextListener (that should throw an exception during webapp-startup):
package de.flexsecure.test.tomcat.embedded;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

@WebListener
public class ContextListener implements ServletContextListener {
	
	@Override
	public final void contextInitialized(ServletContextEvent event) {
		throw new RuntimeException("must happen");
	}

	@Override
	public void contextDestroyed(ServletContextEvent sce) {
		
	}

}
Comment 3 Mark Thomas 2012-09-21 09:14:16 UTC
Where is the listener class located? If it isn't in WEB-INF/classes and it isn't in a JAR on the class path, you'll probably need:

((StandardJarScanner) ctx.getJarScanner()).setScanAllDirectories(true);
Comment 4 Manuel Hartl 2012-09-21 10:01:51 UTC
thank you very much - my Problem is resolved.

maybe it makes sense to add this to a documentation? (or FAQ)?
Comment 5 Mark Thomas 2012-09-27 18:45:51 UTC
Not an issue. The behaviour of the JarScanner is documented. Suggestions for documentation patches welcome.