學Servlet中,突然發現啟動Tomcat時出現這個問題
學習servlet盡然能碰到服務器起不來的,百思不得其解,然後我百度,我FQgoogle,找stackoverflow。找到了不少答案,比如stackoverflow給出說是在
<workspace-directory>\.metadata\.plugins\org.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中的映射,也可以選擇將自動生成的注解注釋掉,這樣就不會有沖突了,二者選其一留下即可。
當然,出現這個問題的原因肯定不止我這一個,肯定有其他原因,推薦還是參考下stackoverflow的吧,百度就算了,浪費時間。