问题
While developing a Java EE application (school exercise), everything seem to be working properly. But since i've added a new servlet on my eclipse project, my tomcat refuses to start and the restart process runs out of time. Even extending the time limit to complete server operations doesnt help neither.
This problem seem to be caused by my web.xml file because when i remove the tags , everything runs like a clockwork. Althoug I'm able to launch my servlet by right clicking on it but that is not the purpose. I need this web.xml to work in order to call my servlet with my jsp with the URL pattern.
The logs only show that the Server prepares for startup, but no error is shown there. If you dont mind taking a look, here is my the code that is causing me trouble. My web.xml is populated with exactly the same as this one and none of them are preventing tomcat to start properly.
<servlet>
<servlet-name>BookYourFlightServlet</servlet-name>
<servlet-class>com.sdzee.servlets.BookYourFlightServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>BookYourFlightServlet</servlet-name>
<url-pattern>/bookYourFlightServlet</url-pattern>
</servlet-mapping>
I also moved my project to my Ubuntu computer in order to test it there but same thing.
It failed again : Here is the log report:
avr. 10, 2013 9:28:25 PM org.apache.catalina.core.AprLifecycleListener init
INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: C:\Program Files\Java\jre7\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;.
avr. 10, 2013 9:28:25 PM org.apache.tomcat.util.digester.SetPropertiesRule begin
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:pro' did not find a matching property.
avr. 10, 2013 9:28:25 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-bio-8080"]
avr. 10, 2013 9:28:25 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-bio-8009"]
avr. 10, 2013 9:28:25 PM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 1015 ms
avr. 10, 2013 9:28:25 PM org.apache.catalina.core.StandardService startInternal
INFO: Démarrage du service Catalina
avr. 10, 2013 9:28:25 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/7.0.39
avr. 10, 2013 9:28:26 PM org.apache.catalina.loader.WebappClassLoader validateJarFile
INFO: validateJarFile(C:\Users\moi\Desktop\mes workspaces\WorkSpace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\pro\WEB-INF\lib\servlet-api.jar) - jar not loaded. See Servlet Spec 2.3, section 9.7.2. Offending class: javax/servlet/Servlet.class
avr. 10, 2013 9:28:26 PM 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].StandardContext[/pro]]
at java.util.concurrent.FutureTask$Sync.innerGet(Unknown Source)
at java.util.concurrent.FutureTask.get(Unknown Source)
at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:1123)
at org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:800)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
at java.util.concurrent.FutureTask$Sync.innerRun(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: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/pro]]
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:154)
... 7 more
Caused by: java.lang.IllegalArgumentException: The servlets named [BookFlightServlet] and [com.sdzee.servlets.BookFlightServlet] are both mapped to the url-pattern [/bookFlightServlet] which is not permitted
at org.apache.catalina.deploy.WebXml.addServletMapping(WebXml.java:335)
at org.apache.catalina.startup.ContextConfig.processAnnotationWebServlet(ContextConfig.java:2457)
at org.apache.catalina.startup.ContextConfig.processAnnotationsStream(ContextConfig.java:2139)
at org.apache.catalina.startup.ContextConfig.processAnnotationsFile(ContextConfig.java:2100)
at org.apache.catalina.startup.ContextConfig.processAnnotationsFile(ContextConfig.java:2093)
at org.apache.catalina.startup.ContextConfig.processAnnotationsFile(ContextConfig.java:2093)
at org.apache.catalina.startup.ContextConfig.processAnnotationsFile(ContextConfig.java:2093)
at org.apache.catalina.startup.ContextConfig.webConfig(ContextConfig.java:1300)
at org.apache.catalina.startup.ContextConfig.configureStart(ContextConfig.java:878)
at org.apache.catalina.startup.ContextConfig.lifecycleEvent(ContextConfig.java:369)
at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:119)
at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:90)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5269)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
... 7 more
avr. 10, 2013 9:28:26 PM 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(Unknown Source)
at java.util.concurrent.FutureTask.get(Unknown Source)
at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:1123)
at org.apache.catalina.core.StandardEngine.startInternal(StandardEngine.java:302)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.StandardService.startInternal(StandardService.java:443)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.StandardServer.startInternal(StandardServer.java:732)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.startup.Catalina.start(Catalina.java:684)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:322)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:456)
Caused by: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost]]
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:154)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
at java.util.concurrent.FutureTask$Sync.innerRun(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: org.apache.catalina.LifecycleException: A child container failed during start
at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:1131)
at org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:800)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
... 7 more
avr. 10, 2013 9:28:26 PM org.apache.catalina.startup.Catalina start
SEVERE: Catalina.start:
org.apache.catalina.LifecycleException: Failed to start component [StandardServer[8005]]
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:154)
at org.apache.catalina.startup.Catalina.start(Catalina.java:684)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:322)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:456)
Caused by: org.apache.catalina.LifecycleException: Failed to start component [StandardService[Catalina]]
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:154)
at org.apache.catalina.core.StandardServer.startInternal(StandardServer.java:732)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
... 7 more
Caused by: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina]]
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:154)
at org.apache.catalina.core.StandardService.startInternal(StandardService.java:443)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
... 9 more
Caused by: org.apache.catalina.LifecycleException: A child container failed during start
at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:1131)
at org.apache.catalina.core.StandardEngine.startInternal(StandardEngine.java:302)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
... 11 more
avr. 10, 2013 9:28:26 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 745 ms
My Servlet:
package com.sdzee.servlets;
import java.io.IOException;
import com.sdzee.beans.ReservationBean;
import com.sdzee.dao.DAOFactory;
import com.sdzee.dao.UtilisateurDao;
import com.sdzee.forms.BookFlightForm;
@WebServlet("/bookFlightServlet")
public class BookFlightServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private static final String VIEW = "/WEB-INF/JSP/bookYourFlight.jsp";
public static final String ATT_BOOKING = "booking";
public static final String ATT_FORM = "form";
public static final String CONF_DAO_FACTORY = "daofactory";
private UtilisateurDao utilisateurDao;
// ========================================================================
public void init() throws ServletException {
/* Récupération d'une instance de notre DAO Utilisateur */
this.utilisateurDao = ((DAOFactory) getServletContext().getAttribute(
CONF_DAO_FACTORY)).getUtilisateurDao();
}
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
/* Affichage de la page d'inscription */
this.getServletContext().getRequestDispatcher(VIEW)
.forward(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
BookFlightForm form = new BookFlightForm(utilisateurDao);
/* Traitement de la requête et récupération du bean en résultant */
ReservationBean reservationBean = form.registerFlightMethod(request);
request.setAttribute(ATT_FORM, form);
request.setAttribute(ATT_BOOKING, reservationBean);
this.getServletContext().getRequestDispatcher(VIEW)
.forward(request, response);
System.out.println("Dopost method");
}
}
My Jsp file
<%@ page pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Book Flight</title>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<link type="text/css" rel="stylesheet" href="styles/form.css" />
</head>
<body>
<form method="post" action="bookFlightServlet">
<fieldset>
<legend>Reservation</legend>
<p>You can book your flight with this form.</p>
<label for="country">Country <span class="requis">*</span></label>
<input type="text" id="country" name="country" value="<c:out value="${reservationBean.country}"/>" size="20" maxlength="60" />
<span class="erreur">${form.erreurs['country']}</span>
<br />
<label for="departureDay">Departure Date <span class="requis">*</span></label>
<input type="text" id="departureDay" name="departureDay" value="<c:out value="${reservationBean.departureDay}"/>" size="20" maxlength="60" />
<span class="erreur">${form.erreurs['departureDay']}</span>
<br />
<input type="submit" value="BOOK" class="sansLabel" />
<br />
<p class="${empty form.erreurs ? 'succes' : 'erreur'}">${form.resultat}</p>
</fieldset>
</form>
</body>
</html>
Web.xml file
<servlet>
<servlet-name>BookFlightServlet</servlet-name>
<servlet-class>com.sdzee.servlets.BookFlightServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>BookFlightServlet</servlet-name>
<url-pattern>/bookFlightServlet</url-pattern>
</servlet-mapping>
回答1:
The startup fails because of this exception.
The servlets named [BookFlightServlet] and [com.sdzee.servlets.BookFlightServlet] are both mapped to the url-pattern [/bookFlightServlet] which is not permitted
You cannot handle a HTTP request with 2 servlets. Use unique names for the servlet-name tags and map the URL patterns to separate servlet classes. The reason you cannot do this is that there is no way to meaningfully commit a HTTP response across 2 servlet threads at the same time.
There is a related thread on SO where someone makes a similar mistake. You can learn from that too.
回答2:
I think you are declaring the Servlet com.sdzee.servlets.BookFlightServlet twice. And by mapping the same servlets (with different names: one with its package and another without) with the same url-apptern, container will fail to start because this is not allowed!!
来源:https://stackoverflow.com/questions/15934260/tomcat-7-refuses-to-start-on-eclipse-when-adding-new-servlet-in-web-xml-dd