简体   繁体   English

嵌入式 Jetty 中的 Web 应用程序出现错误 404 未找到

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

I want to deploy a Java Netbeans Webapp with an embedded Jetty Server;我想部署一个带有嵌入式 Jetty 服务器的 Java Netbeans Webapp; the server itself works, but I always get the following error:服务器本身可以工作,但我总是收到以下错误:

在此处输入图片说明

I searched through mounds of examples on the web, configured & reconfigured my web.xml;我在网上搜索了大量示例,配置并重新配置了我的 web.xml; although my configuration seems fine, I can't get it to work.虽然我的配置看起来不错,但我无法让它工作。

I should point out, that when I run the app from whithin Netbeans using the built-in Glassfish server, it works fine, which tells me that web.xml is probably configured well.我应该指出,当我使用内置 Glassfish 服务器从 whithin Netbeans 运行应用程序时,它运行良好,这告诉我 web.xml 可能配置良好。

Can anyone help with this?任何人都可以帮忙吗?

My code follows.我的代码如下。

PS I know that it's been asked on SO, but those examples did not work for me either. PS我知道有人问过SO,但这些例子对我也不起作用。

Project structure:项目结构:

在此处输入图片说明

WebContext setup: 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: 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 (main method) : 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 ...");
            }
        });
    }
}

Web.xml:网页.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>

Thanks in advance for the help!预先感谢您的帮助!

change from public class AppContextBuilder {从公共类 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;
 }
}

to

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

try this one let me know试试这个让我知道

Is there any stack trace logs available?是否有可用的堆栈跟踪日志?

Without more logs, I can only imagine based on experience that it may be caused by directory resolving, it worked fine in your IDE since it can find the proper files without any further context configuration staff ,which may be not the case when you carry out a real deployment.没有更多的日志,我只能根据经验想象它可能是由目录解析引起的,它在您的IDE中运行良好,因为它可以在没有任何上下文配置人员的情况下找到正确的文件,而在您执行时可能不是这种情况真正的部署。

Maybe the url-pattern is wrong, try to change to something like:也许 url-pattern 是错误的,尝试更改为:

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

Change改变

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

to

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM