在eclipse 中新建Dynamic Web Project时如果之间点击finish,项目中会没有web.xml,但是不会影响本地的项目运行,如果需要web.xml则需要在新建时点击next,最后勾选生成web.xml。
那么没有web.xml,servlet的对应关系在哪呢?我今天就遇到了这样的问题,将本地的项目部署到服务器上时,报找不到servlet的错误,所以需要考一个web.xml到WEB-INF下,并且在下面添加servlet的映射关系:
<servlet>上面是一个servlet的配置:红色的地方是可以随便起的一个名字,但是<servlet>标签中的和<servlet-mapping>中的要一致,蓝色部分是servlet所在的包名(包名.servlet).但是重启tomcat之后报如下错误:
<servlet-name><span style="color:#ff0000;">Sevlet1</span></servlet-name>
<servlet-class><span style="color:#3333ff;">com.Sevlet</span>.InfoSevlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name><span style="color:#ff0000;">Sevlet1</span></servlet-name>
<url-pattern>/InfoSevlet</url-pattern>
</servlet-mapping>
org.apache.catalina.LifecycleException:<span style="color:#ff0000;"> Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/InfoManager]]</span>网上查了好久才知道原因如下:
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:154)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:725)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:701)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:717)
at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:586)
at org.apache.catalina.startup.HostConfig$DeployDescriptor.run(HostConfig.java:1750)
at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
at java.util.concurrent.FutureTask.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.IllegalArgumentException: <span style="color:#ff0000;">The servlets named [Sevlet1] and [com.Sevlet.InfoSevlet] are both mapped to the url-pattern [/InfoSevlet] which is not permitted</span>
at org.apache.tomcat.util.descriptor.web.WebXml.addServletMapping(WebXml.java:308)
at org.apache.catalina.startup.ContextConfig.processAnnotationWebServlet(ContextConfig.java:2342)
at org.apache.catalina.startup.ContextConfig.processAnnotationsStream(ContextConfig.java:2024)
at org.apache.catalina.startup.ContextConfig.processAnnotationsWebResource(ContextConfig.java:1918)
at org.apache.catalina.startup.ContextConfig.processAnnotationsWebResource(ContextConfig.java:1913)
at org.apache.catalina.startup.ContextConfig.processAnnotationsWebResource(ContextConfig.java:1913)
at org.apache.catalina.startup.ContextConfig.webConfig(ContextConfig.java:1139)
at org.apache.catalina.startup.ContextConfig.configureStart(ContextConfig.java:771)
at org.apache.catalina.startup.ContextConfig.lifecycleEvent(ContextConfig.java:305)
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:5066)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
... 10 more
十一月 04, 2016 7:59:39 下午 org.apache.catalina.startup.HostConfig deployDescriptor
严重: Error deploying configuration descriptor D:\eclipse\work\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\conf\Catalina\localhost\InfoManager.xml
java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/InfoManager]]
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:729)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:701)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:717)
at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:586)
at org.apache.catalina.startup.HostConfig$DeployDescriptor.run(HostConfig.java:1750)
at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
at java.util.concurrent.FutureTask.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
新建servlet时它会自动加一个url-pattern声明,所以才会在没有web.xml中配置信息的情况找到对应的servlet。
这样如果你在web.xml中再加上映射关系的话就会重复。所以删除servlet里的声明即可。