故事由来:
学Servlet中,突然发现启动Tomcat时出现这个问题
故事梗概:
学习servlet尽然能碰到服务器起不来的,百思不得其解,然后我百度,我FQgoogle,找stackoverflow。找到了不少答案,比如stackoverflow给出说是在
<workspace-directory>.metadata.pluginsorg.eclipse.core.resources
中有个.snap文件删掉,就可解决这个问题,我试了不行。stackoverflow的链接如下:http://stackoverflow.com/questions/13244233/server-tomcat-v7-0-server-at-localhost-failed-to-start-without-stack-trace-whi。
百度下,Myexception网给出答案是1、端口被占用,2、启动时间不够,3、eclipse引发的问题,4、重新启动电脑(最笨的办法,如果不停的出现,不停的重起会烦死的。)。下面是原文链接。这答案给的没有一个是我要的,所以,以后还是尽量谷歌,百度感觉真心不靠谱。
故事真相:
在eclipse这个强大的软件中,我们省略很多手动写代码的时间,过程如下:
在上图中点击finish后就生成下面的代码。
比如在eclipse中,我们建一个新的 servlet文件后,代码中会生成如下一大块代码。
1 import java.io.IOException; 2 import javax.servlet.ServletException; 3 import javax.servlet.annotation.WebServlet; 4 import javax.servlet.http.HttpServlet; 5 import javax.servlet.http.HttpServletRequest; 6 import javax.servlet.http.HttpServletResponse; 7 8 /** 9 * Servlet implementation class ServletDemo 10 */ 11 @WebServlet("/ServletDemo") 12 public class ServletDemo extends HttpServlet { 13 private static final long serialVersionUID = 1L; 14 15 /** 16 * @see HttpServlet#HttpServlet() 17 */ 18 public ServletDemo() { 19 super(); 20 // TODO Auto-generated constructor stub 21 } 22 23 /** 24 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) 25 */ 26 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 27 // TODO Auto-generated method stub 28 response.getWriter().append("Served at: ").append(request.getContextPath()); 29 } 30 31 /** 32 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) 33 */ 34 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 35 // TODO Auto-generated method stub 36 doGet(request, response); 37 } 38 39 }
而这里面,留意下有个注解
@WebServlet("/ServletDemo")
这个注解是eclipse帮我们生成好的,而问题恰恰就出在这。我们知道,每次我们新建一个servlet后会在web.xml文件中配置映射关系,这样才能在浏览器敲入一个地址后,地址发来的请求能让服务器接收到请求并将该请求交给指定的Servlet去处理。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"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> <servlet> <servlet-name>ServletDemo</servlet-name> <servlet-class>com.vivizhang.servlet.HelloWorldServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>ServletDemo</servlet-name> <url-pattern>/HelloServlet</url-pattern> </servlet-mapping> </web-app>
所以问题的真相就是配置和自动生成的注解冲突了,导致如下的错误(截取):
A child container failed during start java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/ServletDemo]] at java.util.concurrent.FutureTask$Sync.innerGet(FutureTask.java:252) at java.util.concurrent.FutureTask.get(FutureTask.java:111) at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:1120) at org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:819) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:147) at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1572) at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1562) at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334) at java.util.concurrent.FutureTask.run(FutureTask.java:166) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) at java.lang.Thread.run(Thread.java:722) Caused by: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/HelloServlet]] at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:153) ... 7 more Caused by: java.lang.IllegalArgumentException: The servlets named [ServletDemo] and [com.vivizhang.servlet.ServletDemo] are both mapped to the url-pattern [/ServletDemo] which is not permitted at org.apache.catalina.deploy.WebXml.addServletMapping(WebXml.java:293) at org.apache.catalina.startup.ContextConfig.processAnnotationWebServlet(ContextConfig.java:2462) at org.apache.catalina.startup.ContextConfig.processAnnotationsStream(ContextConfig.java:2137) at org.apache.catalina.startup.ContextConfig.processAnnotationsFile(ContextConfig.java:2098) at org.apache.catalina.startup.ContextConfig.processAnnotationsFile(ContextConfig.java:2090) at org.apache.catalina.startup.ContextConfig.processAnnotationsFile(ContextConfig.java:2090) at org.apache.catalina.startup.ContextConfig.processAnnotationsFile(ContextConfig.java:2090) at org.apache.catalina.startup.ContextConfig.processAnnotationsFile(ContextConfig.java:2090) at org.apache.catalina.startup.ContextConfig.webConfig(ContextConfig.java:1304) at org.apache.catalina.startup.ContextConfig.configureStart(ContextConfig.java:889) at org.apache.catalina.startup.ContextConfig.lifecycleEvent(ContextConfig.java:386) at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:117) at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:90) at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5472) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:147) ... 7 more 九月 18, 2016 4:40:37 下午 org.apache.catalina.core.ContainerBase startInternal SEVERE: A child container failed during start java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost]] at java.util.concurrent.FutureTask$Sync.innerGet(FutureTask.java:252) at java.util.concurrent.FutureTask.get(FutureTask.java:111) at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:1120) at org.apache.catalina.core.StandardEngine.startInternal(StandardEngine.java:300) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:147) at org.apache.catalina.core.StandardService.startInternal(StandardService.java:444) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:147) at org.apache.catalina.core.StandardServer.startInternal(StandardServer.java:738) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:147) at org.apache.catalina.startup.Catalina.start(Catalina.java:693) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:601) at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:294) at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:428) Caused by: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost]] at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:153) at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1572) at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1562) at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334) at java.util.concurrent.FutureTask.run(FutureTask.java:166) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) at java.lang.Thread.run(Thread.java:722) Caused by: org.apache.catalina.LifecycleException: A child container failed during start at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:1128) at org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:819) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:147) ... 7 more 九月 18, 2016 4:40:37 下午 org.apache.catalina.startup.Catalina start SEVERE: The required Server component failed to start so Tomcat is unable to start. org.apache.catalina.LifecycleException: Failed to start component [StandardServer[8006]] at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:153) at org.apache.catalina.startup.Catalina.start(Catalina.java:693) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:601) at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:294) at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:428) Caused by: org.apache.catalina.LifecycleException: Failed to start component [StandardService[Catalina]] at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:153) at org.apache.catalina.core.StandardServer.startInternal(StandardServer.java:738) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:147) ... 7 more Caused by: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina]] at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:153) at org.apache.catalina.core.StandardService.startInternal(StandardService.java:444) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:147) ... 9 more Caused by: org.apache.catalina.LifecycleException: A child container failed during start at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:1128) at org.apache.catalina.core.StandardEngine.startInternal(StandardEngine.java:300) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:147) ... 11 more
这里的问题出在:
The servlets named [ServletDemo] and [com.vivizhang.servlet.ServletDemo] are both mapped to the url-pattern [/ServletDemo] which is not permitted
故事解决办法:
可以选择删除web.xml中的映射,也可以选择将自动生成的注解注释掉,这样就不会有冲突了,二者选其一留下即可。
故事后续:
如果在其他问题都没出错的情况下,可能就是xml文件和注解出现了冲突,当然,出现这个问题的原因不止这一个,会有其他很多原因,去google吧,希望我的文章能帮助你解决实际的问题。