0

我正在尝试使用 javalite activeweb 生成一个 jetty 9.4.8 嵌入式 webapp,它是一个基于 servletFilter 的框架。

我用 webappContextHandler 开发了一个服务器主程序,但我总是得到那个异常:

2018-03-07 18:28:54 DEBUG AbstractLifeCycle:177 - STARTED @2308ms default@5c13d641==org.eclipse.jetty.servlet.DefaultServlet,jsp=null,order=0,inst=false
2018-03-07 18:28:54 DEBUG ServletHolder:664 - Servlet.init org.eclipse.jetty.servlet.DefaultServlet@6e75aa0d for default
2018-03-07 18:28:54 INFO  ServletHolder:621 - unavailable
java.lang.NullPointerException
    at org.eclipse.jetty.servlet.DefaultServlet.getInitParameter(DefaultServlet.java:376)
    at org.eclipse.jetty.servlet.DefaultServlet.getInitBoolean(DefaultServlet.java:385)
    at org.eclipse.jetty.servlet.DefaultServlet.init(DefaultServlet.java:182)
    at javax.servlet.GenericServlet.init(GenericServlet.java:244)
    at org.eclipse.jetty.servlet.ServletHolder.initServlet(ServletHolder.java:665)
    at org.eclipse.jetty.servlet.ServletHolder.initialize(ServletHolder.java:423)
    at org.eclipse.jetty.servlet.ServletHandler.initialize(ServletHandler.java:760)
    at org.eclipse.jetty.servlet.ServletContextHandler.startContext(ServletContextHandler.java:348)
    at org.eclipse.jetty.webapp.WebAppContext.startWebapp(WebAppContext.java:1515)
    at org.eclipse.jetty.webapp.WebAppContext.startContext(WebAppContext.java:1477)
    at org.eclipse.jetty.server.handler.ContextHandler.doStart(ContextHandler.java:785)
    at org.eclipse.jetty.servlet.ServletContextHandler.doStart(ServletContextHandler.java:261)
    at org.eclipse.jetty.webapp.WebAppContext.doStart(WebAppContext.java:545)
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
    at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:133)
    at org.eclipse.jetty.server.Server.start(Server.java:418)
    at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:107)
    at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:113)
    at org.eclipse.jetty.server.Server.doStart(Server.java:385)
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
    at app.server.Server.main(Server.java:80)
2018-03-07 18:28:54 DEBUG ServletHandler:764 - EXCEPTION 

我的实际近似是使用 webappcontext,但我正在尝试使用其他类型的处理程序:

     public static void main(String[] args) throws Exception {

            server = new org.eclipse.jetty.server.Server(8000);
            WebAppContext contextHandler = new WebAppContext();

        contextHandler.setContextPath("/");
        contextHandler.setWar("target/classes/WEB-INF");
        FilterHolder filter = contextHandler.addFilter(org.javalite.activeweb.RequestDispatcher.class, "/",  EnumSet.of(DispatcherType.REQUEST));
        filter.setInitParameter("exclusions", "css,images,js,ico,woff,svg,png");
        filter.setInitParameter("root_controller", "home");

        //session
        final DefaultSessionIdManager idmgr = new DefaultSessionIdManager(server);
        idmgr.setServer(server);
        server.setSessionIdManager(idmgr);
        JDBCSessionDataStore sessionStore = new JDBCSessionDataStore();
        DatabaseAdaptor databaseAdaptor = new DatabaseAdaptor();
        databaseAdaptor.setDatasource(dataSource);
        sessionStore.setDatabaseAdaptor(databaseAdaptor);
        SessionHandler sessionHandler = new SessionHandler();
        DefaultSessionCache cacheSession = new DefaultSessionCache(sessionHandler);
        sessionHandler.setSessionCache(cacheSession);
        cacheSession.setSessionDataStore(sessionStore);
        sessionHandler.setSessionIdManager(idmgr);
        contextHandler.setHandler(sessionHandler);

        server.setHandler(contextHandler);

        try {
            server.start();
            server.dumpStdErr();
            server.join();
            System.out.println("finalling main");
        } catch (Exception t) {
            t.printStackTrace();
        } finally {
            if (dataSource != null && !dataSource.isClosed()) {
                dataSource.close();
            }
            if (server != null && server.isRunning()) {
                server.setStopTimeout(10);
                server.stop();
            }
            System.out.println("finalled");
        }

    }

有任何想法吗?

我的实际行家是:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany</groupId>
<artifactId>BeLoyal</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
</properties>


<build>
    <resources>
        <resource>
            <directory>src/main/webapp</directory>
        </resource>
        <resource>
            <directory>src/main/resources</directory>
        </resource>
    </resources>
    <plugins>
        <plugin>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-maven-plugin</artifactId>
            <version>9.4.1.v20170120</version>
            <configuration>
                <reload>manual</reload>
                <scanIntervalSeconds>10000</scanIntervalSeconds>
                <systemProperties>
                    <systemProperty>
                        <name>activejdbc.log</name>
                        <value></value>
                    </systemProperty>
                    <systemProperty>
                        <name>active_reload</name>
                        <value>true</value>
                    </systemProperty>
                    <systemProperty>
                        <name>activeweb.log.request</name>
                        <value>true</value>
                    </systemProperty>
                </systemProperties>

            </configuration>
        </plugin>
        <plugin>
            <groupId>org.javalite</groupId>
            <artifactId>activejdbc-instrumentation</artifactId>
            <version>2.0</version>
            <executions>
                <execution>
                    <phase>process-classes</phase>
                    <goals>
                        <goal>instrument</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>


    </plugins>
</build>
<dependencies>
    <dependency>
        <groupId>com.zaxxer</groupId>
        <artifactId>HikariCP</artifactId>
        <version>2.7.8</version>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>org.javalite</groupId>
        <artifactId>activeweb</artifactId>
        <version>2.0</version>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.35</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>1.7.24</version>
    </dependency>  
    <dependency>
        <groupId>com.sun</groupId>
        <artifactId>tools</artifactId>
        <version>1.7.0</version>
        <scope>system</scope>
        <systemPath>${java.home}/../lib/tools.jar</systemPath>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.eclipse.jetty/jetty-webapp -->
    <dependency>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>jetty-webapp</artifactId>
        <version>9.4.8.v20171121</version>
    </dependency>

</dependencies>

欢迎提出建议

编辑1:

实际上我尝试:

 ServletContextHandler contextHandler = new ServletContextHandler(ServletContextHandler.SESSIONS);
    contextHandler.setContextPath("/");
    contextHandler.setHandler(sessionHandler);

    ServletHandler servletHandler = new ServletHandler();
    FilterHolder filter = servletHandler.addFilter("org.javalite.activeweb.RequestDispatcher", "/", EnumSet.of(DispatcherType.REQUEST));
    filter.setInitParameter("exclusions", "css,images,js,ico,woff,svg,png");
    filter.setInitParameter("root_controller", "home");

    HandlerCollection collectionHandler = new HandlerCollection(servletHandler,contextHandler);

    server.setHandler(collectionHandler);

异常消失,但是当调用网页 freemarker 时说找不到模板,因为 servletcontext 为空

4

2 回答 2

0

看起来你在这一行有错字:

contextHandler.setWar("target/classes/WEB-INF");

它需要指向一个war文件的根目录。

见: http: //www.eclipse.org/jetty/documentation/9.3.x/embedding-jetty.html

您还可以将 Jetty 源代码加载到调试器中(它是开源的,您知道 :))并查看问题所在。

更新:

我花了 10 分钟和 7 行代码来完成这项工作。请在这里查看示例:https ://github.com/javalite/activeweb-simple/blob/master/src/main/java/app/Main.java

于 2018-03-07T18:12:49.940 回答
0

终于我实现了。

我在以下位置发布了一个示例:

https://github.com/devMls/base_embedded_jetty

如果它可以帮助某人。

于 2018-03-09T17:08:57.497 回答