2

在开发 Java EE 应用程序(学校练习)时,一切似乎都运行正常。但是由于我在我的 eclipse 项目中添加了一个新的 servlet,我的 tomcat 拒绝启动并且重启过程超时。即使延长完成服务器操作的时间限制也无济于事。

这个问题似乎是由我的 web.xml 文件引起的,因为当我删除标签时,一切都像发条一样运行。虽然我可以通过右键单击它来启动我的 servlet,但这不是目的。我需要这个 web.xml 才能使用带有 URL 模式的 jsp 调用我的 servlet。

日志仅显示服务器准备启动,但没有显示错误。如果您不介意看一下,这是我的代码,它给我带来了麻烦。我的 web.xml 填充了与这个完全相同的内容,并且它们都没有阻止 tomcat 正常启动。

<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>

我还将我的项目移到了我的 Ubuntu 计算机上,以便在那里进行测试,但同样的事情。

它再次失败:这是日志报告:

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

我的小服务程序:

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");

    }

}

我的 Jsp 文件

<%@ 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 文件

<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>
4

2 回答 2

1

由于此异常,启动失败。

名为 [BookFlightServlet] 和 [com.sdzee.servlets.BookFlightServlet] 的 servlet 都映射到不允许的 url 模式 [/bookFlightServlet]

您无法使用 2 个 servlet 处理 HTTP 请求。为 servlet-name 标记使用唯一名称,并将 URL 模式映射到单独的 servlet 类。您不能这样做的原因是无法同时跨 2 个 servlet 线程有意义地提交 HTTP 响应。

SO上有一个相关的线程,有人犯了类似的错误。你也可以从中学习。

于 2013-04-10T19:47:38.630 回答
1

我认为您两次声明了 Servlet com.sdzee.servlets.BookFlightServlet。通过使用相同的 url-apptern 映射相同的 servlet(具有不同的名称:一个带有其包,另一个没有),容器将无法启动,因为这是不允许的!

于 2013-04-10T19:54:00.080 回答