jetty的使用

首先jetty是啥呢?Jetty 是一个开源的servlet容器,它为基于Java的web容器,例如JSP和servlet提供运行环境。Jetty是使用Java语言编写的,它的API以一组JAR包的形式发布。开发人员可以将Jetty容器实例化成一个对象,可以迅速为一些独立运行(stand-alone)的Java应用提供网络和web连接。——百度百科

说白了,jetty可以像Tomcat一样让运行web应用。

有什么好处呢?jetty可以很方便的设置项目名和访问端口,在你需要启动多个项目的时候就很方便了。

怎么使用呢?接下来说说怎么在项目中嵌入jetty

首先是pom文件中需要添加:

        <dependency>
			<groupId>org.eclipse.jetty</groupId>
			<artifactId>jetty-server</artifactId>
			<version>9.2.10.v20150310</version>
			<exclusions>
				<exclusion>
					<groupId>log4j</groupId>
					<artifactId>log4j</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
  		<dependency>
			<groupId>org.eclipse.jetty</groupId>
			<artifactId>jetty-webapp</artifactId>
			<version>9.2.10.v20150310</version>
			<exclusions>
				<exclusion>
					<groupId>log4j</groupId>
					<artifactId>log4j</artifactId>
				</exclusion>
			</exclusions>
		</dependency>

其次是jetty的启动类:

/**
 * 
 *@description 
 *@auther panmingshuai
 *@time 2018年4月7日下午3:41:18
 *
 */
public class JettyServer {
	private static final Logger LOGGER = Logger.getLogger(JettyServer.class);
	private int port;
	private String contextPath;
	private String webappPath;
	private Server server;
	private WebAppContext webapp;

	/**
	 * 创建用于开发运行调试的Jetty Server, 以src/main/webapp为Web应用目录.
	 */
	public static void main(String[] args) {
		JettyServer jettyServer = new JettyServer("/ps", "src/main/webapp", 81);
		jettyServer.start();
	}

	public JettyServer(String contextPath, String webappPath, int port) {
		if (StringUtils.isBlank(contextPath)) {
			this.contextPath = "";
		} else {
			this.contextPath = contextPath;
		}
		if (StringUtils.isBlank(webappPath)) {
			throw new IllegalArgumentException("webappPath is required");
		} else {
			this.webappPath = webappPath;
		}
		this.port = port;
	}

	public void start() {
		if (null == server || server.isStopped()) {
			doStart();
		} else {
			throw new IllegalStateException("JettyServer already started.");
		}
	}

	private void doStart() {
		if (!checkServerPortAvailable()) {
			throw new IllegalStateException("Server port already in use: " + port);
		}

		server = new Server();
		// 设置在JVM退出时关闭Jetty的钩子。
		server.setStopAtShutdown(true);
		// 这是http的连接器
		ServerConnector connector = new ServerConnector(server);
		connector.setPort(port);
		// 解决Windows下重复启动Jetty居然不报告端口冲突的问题.
		connector.setReuseAddress(false);
		server.setConnectors(new Connector[] { connector });

		webapp = new WebAppContext(webappPath, contextPath);

		server.setHandler(webapp);
		try {
			long st = System.currentTimeMillis();
			server.start();
			long sp = System.currentTimeMillis() - st;
			System.out.println("JettyServer " + Jetty.VERSION + " started: " + String.format("%.2f sec", sp / 1000D)
					+ ",the port is ===" + port + "");
			server.join();
		} catch (Exception e) {
			e.printStackTrace();
			LOGGER.error("JettyServer started failed!");
		}
	}

	private boolean checkServerPortAvailable() {
		if (0 < port) {
			ServerSocket ss = null;
			try {
				ss = new ServerSocket(port, 0, null);
			} catch (Exception e) {
				LOGGER.error("check serverPort failed", e);
				return false;
			} finally {
				if (null != ss) {
					try {
						ss.close();
					} catch (IOException e) {
						LOGGER.error("close ServerSocket failed", e);
					}
				}
			}
		} else {
			throw new IllegalArgumentException("Invalid port " + port);
		}
		return true;
	}

}

需要注意的是:JettyServer jettyServer = new JettyServer("/ps", "src/main/webapp", 81);这个方法中ps是你访问的项目名,81是访问端口,即你要访问项目的地址是:localhost:81/ps

猜你喜欢

转载自my.oschina.net/u/3534905/blog/1790770
今日推荐