嵌入式启动jetty
由于jetty8以上版本已经抛弃JDK1.6,公司统一开发JDK又一直不升级,所以我们使用jetty8
pom.xml
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.nihaorz</groupId>
<artifactId>my-project</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>my-project</name>
<url>http://maven.apache.org</url>
<dependencies>
<!-- 增加对jsp解析的支持,如果项目中没有使用jsp,这个依赖可以删掉 -->
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-jsp</artifactId>
<version>8.1.22.v20160922</version>
</dependency>
<!-- jetty主依赖 -->
<dependency>
<groupId>org.eclipse.jetty.aggregate</groupId>
<artifactId>jetty-all</artifactId>
<version>8.1.22.v20160922</version>
</dependency>
</dependencies>
<build>
<finalName>my-project</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
jetty.properties,依据项目结构自行修改
#jetty部署端口 jetty.port=8080 #jetty部署上下文 jetty.contextPath=/webapp #资源路径,开发模式下默认为./src/main/webapp,也可设置绝对路径 jetty.resourceBase=./src/main/webapp #web.xml路径,开发模式下默认为./src/main/webapp/WEB-INF/web.xml,也可设置绝对路径 jetty.descriptor=./src/main/webapp/WEB-INF/web.xml
DevStart.java,启动类,run main()
package com.nihaorz.start;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.AbstractHandler;
import org.eclipse.jetty.webapp.WebAppContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Properties;
public class DevStart extends AbstractHandler {
@Override
public void handle(String s, Request request, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws IOException, ServletException {
}
public static void main(String[] args) throws Exception {
Properties prop = new Properties();
prop.load(DevStart.class.getResourceAsStream("/config/jetty.properties"));
WebAppContext context = new WebAppContext();
// 设置端口
Server server = new Server(Integer.parseInt(prop.getProperty("jetty.port")));
// 设置部署上下文
context.setContextPath(prop.getProperty("jetty.contextPath"));
// 设置基础资源路径
context.setResourceBase(prop.getProperty("jetty.resourceBase"));
// 设置web.xml
context.setDescriptor(prop.getProperty("jetty.descriptor"));
context.setParentLoaderPriority(true);
server.setHandler(context);
server.start();
server.join();
}
}

浙公网安备 33010602011771号