Java 嵌入式码头的应用显示;“未找到页面”;如果作为jar启动

Java 嵌入式码头的应用显示;“未找到页面”;如果作为jar启动,java,jar,jetty,embedded-jetty,Java,Jar,Jetty,Embedded Jetty,我有一个使用嵌入式jetty的应用程序。当我在Netbeans IDE中运行此应用程序时,我可以浏览我的站点@localhost:8080/ 当我从命令行java-jarapp.jar启动应用程序的jar文件时,浏览localhost:8080/jetty服务器时会显示“找不到页面” 我错过了什么?我想不出这个问题 编辑: Netbeans项目已上载到 如果我在Netbeans中运行这个项目,一切都正常。 但是当我用lib文件夹获取jar文件并在cmd中像这样运行时:java-jarembedd

我有一个使用嵌入式jetty的应用程序。当我在Netbeans IDE中运行此应用程序时,我可以浏览我的站点@localhost:8080/

当我从命令行java-jarapp.jar启动应用程序的jar文件时,浏览localhost:8080/jetty服务器时会显示“找不到页面”

我错过了什么?我想不出这个问题

编辑

Netbeans项目已上载到

如果我在Netbeans中运行这个项目,一切都正常。 但是当我用lib文件夹获取jar文件并在cmd中像这样运行时:java-jarembeddedJettyJSPJSTL.jar

然后导航到“我得到错误”:

org.apache.jasper.jaspereException:java.lang.ClassNotFoundException:org.apache.jsp.WEB\u 002dINF.jstl\u jsp

org.apache.jasper.JasperException:绝对uri:无法在web.xml或与此应用程序一起部署的jar文件中解析

我的JSP页面使用JSTL,看起来好像没有找到JSTL库

这是启动服务器的代码:

package server;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.lang.management.ManagementFactory;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;

import org.eclipse.jetty.jmx.MBeanContainer;
import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.server.handler.AllowSymLinkAliasChecker;
import org.eclipse.jetty.server.handler.DefaultHandler;
import org.eclipse.jetty.server.handler.HandlerList;
import org.eclipse.jetty.webapp.Configuration;
import org.eclipse.jetty.webapp.WebAppContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author lkallas
 */
public class JettyServer {

    // Resource path pointing to where the WEBROOT is
    private static final String WEBROOT = "/webapp/";
    private static final Logger logger = LoggerFactory.getLogger(JettyServer.class);

    public void start() throws IOException, InterruptedException, URISyntaxException {

        Server server = new Server();

        // HTTP connector
        ServerConnector connector = new ServerConnector(server);
        connector.setHost("localhost");
        connector.setPort(8080);
        connector.setIdleTimeout(30000);

        // Set the connector
        server.addConnector(connector);

        // Setup JMX for web applications
        MBeanContainer mbContainer = new MBeanContainer(
                ManagementFactory.getPlatformMBeanServer());
        server.addBean(mbContainer);

        //Setting up web application
        WebAppContext webapp = new WebAppContext();
        webapp.setAttribute("javax.servlet.context.tempdir", getScratchDir());
        webapp.setDescriptor(WEBROOT + "WEB-INF/web.xml");
        webapp.setResourceBase(getWebRootResourceUri().toASCIIString());
        webapp.setContextPath("/");
        webapp.setWar(getWebRootResourceUri().toASCIIString());
        webapp.addAliasCheck(new AllowSymLinkAliasChecker());

        //For debugging
        logger.info("Descriptor file: {}", webapp.getDescriptor());
        logger.info("Resource base: {}", getWebRootResourceUri().toASCIIString());
        logger.info("WAR location: {}", webapp.getWar());

        HandlerList handlerList = new HandlerList();
        handlerList.setHandlers(new Handler[]{webapp, new DefaultHandler()});

        // This webapp will use jsps and jstl. We need to enable the
        // AnnotationConfiguration in order to correctly
        // set up the jsp container
        Configuration.ClassList classlist = Configuration.ClassList
                .setServerDefault(server);
        classlist.addBefore(
                "org.eclipse.jetty.webapp.JettyWebXmlConfiguration",
                "org.eclipse.jetty.annotations.AnnotationConfiguration");

        // Set the ContainerIncludeJarPattern so that jetty examines these
        // container-path jars for tlds, web-fragments etc.
        // If you omit the jar that contains the jstl .tlds, the jsp engine will
        // scan for them instead.
        webapp.setAttribute("org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern", ".*/[^/]*taglibs.*\\.jar$");

        // A WebAppContext is a ContextHandler as well so it needs to be set to
        // the server so it is aware of where to send the appropriate requests.
        server.setHandler(handlerList);

        try {
            server.start();

        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }

        server.dumpStdErr();

    }

    /**
     * Establish Scratch directory for the servlet context (used by JSP
     * compilation)
     */
    private File getScratchDir() throws IOException {
        File tempDir = new File(System.getProperty("java.io.tmpdir"));
        File scratchDir = new File(tempDir.toString(), "embedded-jetty");

        if (!scratchDir.exists()) {
            if (!scratchDir.mkdirs()) {
                throw new IOException("Unable to create scratch directory: " + scratchDir);
            }
        }
        return scratchDir;
    }

    /**
     * Get webroot URI.
     *
     * @return
     * @throws FileNotFoundException
     * @throws URISyntaxException
     */
    private URI getWebRootResourceUri() throws FileNotFoundException, URISyntaxException {
        URL indexUri = this.getClass().getResource(WEBROOT);
        if (indexUri == null) {
            throw new FileNotFoundException("Unable to find resource " + WEBROOT);
        }
        logger.debug("WEBROOT: {}", indexUri.toURI().toASCIIString());
        return indexUri.toURI();
    }

}

我已经看过了@

有很多原因可能会影响你

但是,您尚未发布任何代码来帮助我们确定具体原因

顺便说一句,Jetty项目维护了此设置的一个示例

注意你的
context.setContextPath()
(就像@Haider Ali指出的那样),还有你的
context.setBaseResource()

对于嵌入式Jetty中的JSP,您可以查看另一个示例项目


注意。

我认为URL中缺少上下文路径。你能发布你的Jetty配置吗?我提供了源代码。谢谢。我提供了源代码和gihub存储库。如果你能帮我解决这个问题,那会很有帮助的。非常感谢。哇,JSP,是的,这需要嵌入式jetty做更多的工作。按照Github中显示的方式做每件事,仍然不起作用。同样的错误仍然存在。还尝试使用不同的jstl jar。Jstl只是不使用打包到jar的嵌入式jetty应用程序。我必须删除jstl并使用旧的jsp样式。没有jstl的JSP正在工作。