我正在尝试使用javalite activeweb生成一个jetty 9.4.8嵌入式webapp,这是一个基于servletFilter的框架。
我使用webappContextHandler开发服务器main,但我总是获得该异常:
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
我的实际aproximation是使用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为null
答案 0 :(得分: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
答案 1 :(得分:0)