在本身項目中嵌入Jetty

最近一個項目要求在項目中添加監控頁面的功能, 由於jetty是一個比較輕量級, 適合嵌入的web服務器. java

1:下載jetty包 web

http://repo2.maven.org/maven2/org/eclipse/jetty/ apache

2:導入jetty依賴包 服務器

com.sun.el-2.2.0.v201108011116.jar
javax.el-2.2.0.v201108011116.jar
javax.servlet.jsp-2.2.0.v201112011158.jar
javax.servlet.jsp.jstl-1.2.0.v201105211821.jar
jetty-all-server-8.0.4.v20111024.jar
org.apache.jasper.glassfish-2.2.2.v201112011158.jar
org.apache.taglibs.standard.glassfish-1.2.0.v201112081803.jar


3:編寫代碼 app

package com.zhongsou.jetty;

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.webapp.WebAppContext;
import com.zhongsou.appserver.conf.Config;
import com.zhongsou.appserver.log.AbstractLog;

public class JettyServer extends AbstractLog {

	private Server server;

	@Override
	public boolean init(Config conf) {
		try {
			server = new Server(conf.getJetty().getPort());
			
			WebAppContext webapp = new WebAppContext();
			webapp.setContextPath("/");
			
			webapp.setResourceBase(System.getProperty("JettyWebRoot", "./webapps"));
			server.setHandler(webapp);
		} catch (Exception e) {
			logger.error("JettyServer init", e);
			return false;
		}
		return true;
	}

	@Override
	public boolean start() {
		try {
			server.start();
			return true;
		} catch (Exception e) {
			logger.error("JettyServer start", e);
			return false;
		}
	}
}


參考:http://wiki.eclipse.org/Jetty#Embedding_Jetty eclipse

相關文章
相關標籤/搜索