Embedded Jetty 9.4在IDE中运行,但不是自主的

时间:2017-05-02 13:49:51

标签: jsp jetty

我正在尝试使用嵌入式Jetty运行JSP和Servlet。我有一个标准的Java应用程序(不是Web应用程序),它打包为JAR(而不是WAR),我有一个专用文件夹来存储所有与Web相关的文件(“webui /”):web.xml,* .jsp ,* .jspf等。此文件夹与JAR位于同一文件夹中,可从应用程序访问(已证实)。

当我从NetBeans(版本8.1和Java 8)中运行它时,我的应用程序运行正常,但是当我从shell窗口(java -jar myapp.jar)执行它时,它提供了以下错误

org.apache.jasper.JasperException: /menu.jsp (line: 6, column: 0) The absolute uri: http://java.sun.com/jsp/jstl/core cannot be resolved in either web.xml or the jar files deployed with this application

在发布此内容之前,我已经阅读了以下帖子(以及其他许多内容):

这是我的POM

<!--Jetty dependencies start here -->
    <dependency>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>jetty-server</artifactId>
        <version>${jettyVersion}</version>
    </dependency>
    <dependency>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>jetty-webapp</artifactId>
        <version>${jettyVersion}</version>
    </dependency>
    <dependency>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>jetty-servlet</artifactId>
        <version>${jettyVersion}</version>
    </dependency>
    <dependency>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>jetty-annotations</artifactId>
        <version>${jettyVersion}</version>
    </dependency>

    <!--Jetty Apache JSP  -->
    <dependency>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>apache-jsp</artifactId>
        <version>${jettyVersion}</version>
    </dependency>

    <!-- Jetty JSTL -->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>

这是我的代码

private static Server serverWeb = null;

//----------------------------------------------------------------------------//

public static void Main( String[] args )
{
    JettyExampleStack instance = new JettyExampleStack();
                      instance.initServerWeb();
}

//----------------------------------------------------------------------------//

private void initServerWeb()
{
    WebAppContext ctx = new WebAppContext();
                  ctx.setContextPath( "/" );

    serverWeb = new Server( 8080 );
    serverWeb.setHandler( ctx );
    serverWeb.setStopAtShutdown( true );
    serverWeb.setStopTimeout( UtilConvert.SECOND * 5 );

    try
    {
        initServlets( ctx );
        initJSPs( serverWeb, ctx );

        serverWeb.start();
    }
    catch( Exception exc )
    {
        UtilDebug.log( exc );
        stopdown();
    }
}

private void initServlets( WebAppContext ctx ) throws Exception
{
    ctx.addServlet( GetFile.class, "/getFile" );
}

private void initJSPs( Server server, WebAppContext ctx ) throws Exception
{
    ctx.setResourceBase( "webui" );
    ctx.setDescriptor( "web.xml" );

    // ----------------------------------------------------------------
    // TAKEN FROM HERE:
    // https://github.com/jetty-project/embedded-jetty-jsp
    // ----------------------------------------------------------------

    // By default, the JSP implementation will use an internal eclipse JDT compiler.
    // This will use the JDK JavaC built-in compiler.
    System.setProperty( "org.apache.jasper.compiler.disablejsr199", "false" );

    // Set a Servlet Temp Directory
    File fTmp = new File( System.getProperty( "java.io.tmpdir" ), "jetty-jsp-tmp" );
    if( ! UtilIO.createFolder( fTmp ) )
    {
        throw new IOException( "Can not create folder '"+ fTmp.getAbsolutePath() +"'" );
    }
    ctx.setAttribute("javax.servlet.context.tempdir", fTmp );

    // Set Classloader of Context to be sane (needed for JSTL)
    // JSP requires a non-System classloader, this simply wraps the
    // embedded System classloader in a way that makes it suitable
    // for JSP to use
    ClassLoader jspClassLoader = new URLClassLoader( new URL[0], WebServer.class.getClassLoader() );
    ctx.setClassLoader( jspClassLoader );

    // The JspServlet must be named "jsp" (per JSP spec).
    // Add JSP Servlet (must be named "jsp")
    ServletHolder holderJsp = new ServletHolder( "jsp", JspServlet.class );
                  holderJsp.setInitOrder( 0 );

    // Including the JSTL jars for the webapp.*/
    ctx.setAttribute( "org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern", ".*/[^/]*jstl.*\\.jar$" );

    // Enabling the Annotation based configuration
    Configuration.ClassList classlist = Configuration.ClassList.setServerDefault( server );

    classlist.addBefore( "org.eclipse.jetty.webapp.JettyWebXmlConfiguration",
                         "org.eclipse.jetty.annotations.AnnotationConfiguration" );

    classlist.addAfter( "org.eclipse.jetty.webapp.FragmentConfiguration",
                        "org.eclipse.jetty.plus.webapp.EnvConfiguration",
                        "org.eclipse.jetty.plus.webapp.PlusConfiguration" );
}

非常感谢您的帮助。

0 个答案:

没有答案