I can not start Tomcat when I use web.xml as the configuration file for servlets in the application, this is the contents of the file:
<?xml version="1.0" encoding="UTF-8"?> <web-app id="WebApp_ID" version="3.0" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <servlet> <servlet-name>AppLifeServlet</servlet-name> <servlet-class> com.example.application_lifecycle_chapter_4_7.ApplicatationLifecycleUI.Servlet </servlet-class> <init-param> <param-name>UI</param-name> <param-value>com.example.application_lifecycle_chapter_4_7.ApplicationLifecycleUI</param-value> </init-param> <async-supported>true</async-supported> </servlet> <servlet> <servlet-name>MyPushyServlet</servlet-name> <servlet-class> com.example.application_lifecycle_chapter_4_7.MyPushyUI.Servlet </servlet-class> <init-param> <param-name>UI</param-name> <param-value>com.example.application_lifecycle_chapter_4_7.MyPushyUI</param-value> </init-param> <async-supported>true</async-supported> </servlet> <servlet-mapping> <servlet-name>AppLifeServlet</servlet-name> <url-pattern>/AppLife/*</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>AppLifeServlet</servlet-name> <url-pattern>/VAADIN/*</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>MyPushyServlet</servlet-name> <url-pattern>/MyPushyUI/*</url-pattern> </servlet-mapping> </web-app>
Why doesn't it work? Tomcat doesn't even start and instead throws an exception:
SEVERE: Failed to destroy end point associated with ProtocolHandler ["ajp-nio-8009"] java.lang.NullPointerException at org.apache.tomcat.util.net.NioEndpoint.releaseCaches(NioEndpoint.java:307) at org.apache.tomcat.util.net.NioEndpoint.unbind(NioEndpoint.java:482) at org.apache.tomcat.util.net.AbstractEndpoint.destroy(AbstractEndpoint.java:795) at org.apache.coyote.AbstractProtocol.destroy(AbstractProtocol.java:531) at org.apache.catalina.connector.Connector.destroyInternal(Connector.java:1023) at org.apache.catalina.util.LifecycleBase.destroy(LifecycleBase.java:305) at org.apache.catalina.core.StandardService.destroyInternal(StandardService.java:588) at org.apache.catalina.util.LifecycleBase.destroy(LifecycleBase.java:305) at org.apache.catalina.core.StandardServer.destroyInternal(StandardServer.java:850) at org.apache.catalina.util.LifecycleBase.destroy(LifecycleBase.java:305) at org.apache.catalina.startup.Catalina.start(Catalina.java:629) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:483) at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:351) at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:485)
If I turn off web.xml and I use Servlet 3.0 API annotations, everything works ...
What is the problem with web.xml?
EDIT: Here is the full output of the exception when Tomcat tries to start:
SEVERE: A child container failed during start java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/Application_Lifecycle_Chapter_4.7]] at java.util.concurrent.FutureTask.report(FutureTask.java:122) at java.util.concurrent.FutureTask.get(FutureTask.java:192) at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:917) at org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:868) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1409) at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1399) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) at java.lang.Thread.run(Thread.java:745) Caused by: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/Application_Lifecycle_Chapter_4.7]] at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:154) ... 6 more Caused by: java.lang.IllegalArgumentException: The servlets named [AppLifeServlet] and [com.example.application_lifecycle_chapter_4_7.ApplicationLifecycleUI$Servlet] are both mapped to the url-pattern [/AppLife
RESOLVED: This is the key line:
Caused by: java.lang.IllegalArgumentException: The servlets named [AppLifeServlet] and [com.example.application_lifecycle_chapter_4_7.ApplicationLifecycleUI$Servlet] are both mapped to the url-pattern [/AppLife/*] which is not permitted
From the Book of Vaadin:
You can use both web.xml and WebServlet in one application. Settings in web.xml override those specified in annotations.
This is wrong ! In my case, the error was caused by this phrase: I had the @WebServlet
annotation in my ApplicationLifecycleUI$Servlet
along with the rendering of the servlet inside the web.xml file.
The two configurations cannot coexist together, so you must choose whether to use the web.xml or @WebServlet
to map the URLs, or you will get this error when trying to start Tomcat.