簡體   English   中英

嵌入式 Jetty 中的 Web 應用程序出現錯誤 404 未找到

[英]Webapplication in Embedded Jetty getting Error 404 Not found

我想部署一個帶有嵌入式 Jetty 服務器的 Java Netbeans Webapp; 服務器本身可以工作,但我總是收到以下錯誤:

在此處輸入圖片說明

我在網上搜索了大量示例,配置並重新配置了我的 web.xml; 雖然我的配置看起來不錯,但我無法讓它工作。

我應該指出,當我使用內置 Glassfish 服務器從 whithin Netbeans 運行應用程序時,它運行良好,這告訴我 web.xml 可能配置良好。

任何人都可以幫忙嗎?

我的代碼如下。

PS我知道有人問過SO,但這些例子對我也不起作用。

項目結構:

在此處輸入圖片說明

WebContext 設置:

import org.eclipse.jetty.webapp.WebAppContext;

public class AppContextBuilder {

    private WebAppContext webAppContext;

    public WebAppContext buildWebAppContext() {
        webAppContext = new WebAppContext();
        webAppContext.setDescriptor(webAppContext + "/WEB-INF/web.xml");
        webAppContext.setResourceBase("src/main/webapp");
        webAppContext.setContextPath("/Holmes");

        return webAppContext;
    }
}

JettyServer.java:

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.ContextHandlerCollection;

public class JettyServer {

    private Server server;

    public JettyServer() {
        this(8585);
    }

    public JettyServer(Integer runningPort) {
        server = new Server(runningPort);
    }

    public void setHandler(ContextHandlerCollection contexts) {
        server.setHandler(contexts);
    }

    public void start() throws Exception {
        server.start();
    }

    public void stop() throws Exception {
        server.stop();
        server.join();
    }

    public boolean isStarted() {
        return server.isStarted();
    }

    public boolean isStopped() {
        return server.isStopped();
    }
}

Deploy.java (主要方法)

import org.apache.log4j.Logger;
import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.handler.ContextHandlerCollection;

/**
 *
 * @author Motty Waldner <motty@timeworksny.com>
 */

public class Deploy {

    private final static Logger log = Logger.getLogger(Deploy.class);

    static JettyServer jettyServer = new JettyServer();

    public static void main(String[] args) throws Exception {
        // add hook to stop server upon service termination
        // (service calls System.exit(0) upon termination,
        // so it should work under normal circumstances)
        addShutdownHook();

        ContextHandlerCollection contexts = new ContextHandlerCollection();
        Handler[] handlers = new Handler[]{new AppContextBuilder().buildWebAppContext().getHandler()};
        contexts.setHandlers(handlers);

        jettyServer = new JettyServer();

        try {
            jettyServer.start();
        } catch (Exception e) {
            log.error("Error Starting Jetty Server", e);
        }
    }

    private static void addShutdownHook() {
        Runtime.getRuntime().addShutdownHook(new Thread() {
            @Override
            public void run() {
                try {
                    jettyServer.stop();
                    log.info("Shutdown Hook is running: Jetty Server instance being stopped.");
                } catch (Exception e) {
                    log.error("error", e);
                }

                log.info("Application Terminating ...");
            }
        });
    }
}

網頁.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    <display-name>Test App</display-name>

    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    
    <filter>
        <filter-name> struts2 </filter-name>
        <filter-class>
            org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
        </filter-class>
    </filter>

    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
    </welcome-file-list>
    
    <session-config>  
        <session-timeout>120</session-timeout>  
    </session-config>
  
    <servlet-mapping>
        <servlet-name>StrutsController</servlet-name>
        <url-pattern>*.html</url-pattern>
    </servlet-mapping>
 
</web-app>

預先感謝您的幫助!

從公共類 AppContextBuilder { 更改

private WebAppContext webAppContext;

public WebAppContext buildWebAppContext() {
    webAppContext = new WebAppContext();
    webAppContext.setDescriptor(webAppContext + "/WEB-INF/web.xml");
    webAppContext.setResourceBase("src/main/webapp");
    webAppContext.setContextPath("/Holmes");

    return webAppContext;
 }
}

public class AppContextBuilder {

private WebAppContext webAppContext;

public WebAppContext buildWebAppContext() {
    webAppContext = new WebAppContext();
    webAppContext.setDescriptor(webAppContext + "/WEB-INF/web.xml");
    webAppContext.setResourceBase(webAppContext);
    webAppContext.setContextPath("/Holmes");

    return webAppContext;
  }
}

試試這個讓我知道

是否有可用的堆棧跟蹤日志?

沒有更多的日志,我只能根據經驗想象它可能是由目錄解析引起的,它在您的IDE中運行良好,因為它可以在沒有任何上下文配置人員的情況下找到正確的文件,而在您執行時可能不是這種情況真正的部署。

也許 url-pattern 是錯誤的,嘗試更改為:

    <servlet-mapping>
       <servlet-name>StrutsController</servlet-name>
       <url-pattern>*</url-pattern>
    </servlet-mapping>

改變

Handler[] handlers = new Handler[]{new AppContextBuilder().buildWebAppContext().getHandler()};

Handler[] handlers = new Handler[]{new AppContextBuilder().buildWebAppContext()};

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM