package cn.com.songjy.jetty;
import java.io.BufferedInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.mortbay.jetty.Connector;
import org.mortbay.jetty.Handler;
import org.mortbay.jetty.NCSARequestLog;
import org.mortbay.jetty.Server;
import org.mortbay.jetty.handler.ContextHandlerCollection;
import org.mortbay.jetty.handler.DefaultHandler;
import org.mortbay.jetty.handler.HandlerCollection;
import org.mortbay.jetty.handler.RequestLogHandler;
import org.mortbay.jetty.nio.SelectChannelConnector;
import org.mortbay.jetty.webapp.WebAppContext;
import org.mortbay.xml.XmlConfiguration;
import org.xml.sax.SAXException;
public class JettyDemo {
private static final Log log = LogFactory.getLog(JettyDemo.class);
private static final Properties config = new Properties();
static {
BufferedInputStream bufferedInputStream = null;
try {
bufferedInputStream = new BufferedInputStream(JettyDemo.class
.getClassLoader().getResourceAsStream("jetty.properties"));
config.load(bufferedInputStream);
} catch (IOException e) {
log.error(e.getMessage(), e);
} finally {
try {
if(null != bufferedInputStream)
bufferedInputStream.close();
} catch (IOException e) {
log.error(e.getMessage(), e);
}
}
}
public static void main(String[] args) throws Exception {
jetty_start();
}
public static void start_jetty() throws SAXException, IOException,
FileNotFoundException, Exception {
Server server = new Server();
// 指定自定义的jetty.xml路径
XmlConfiguration configuration = new XmlConfiguration(Server.class
.getClassLoader().getResourceAsStream(
config.getProperty("jetty.xml")));
configuration.configure(server);
server.start();
}
public static void jetty_start() {
try {
String jetty_home = "demo";// 这个就是你的项目发布时候的名字
//用户访问记录
HandlerCollection handlers = new HandlerCollection();
ContextHandlerCollection contexts = new ContextHandlerCollection();
RequestLogHandler requestLogHandler = new RequestLogHandler();
handlers.setHandlers(new Handler[]{contexts,new DefaultHandler(),requestLogHandler});
NCSARequestLog requestLog = new NCSARequestLog("./target/jetty-yyyy_mm_dd.request.log");
requestLog.setRetainDays(90);
requestLog.setAppend(true);
requestLog.setExtended(false);
requestLog.setLogTimeZone("GMT");
requestLogHandler.setRequestLog(requestLog);
Server server = new Server();
Connector connector = new SelectChannelConnector();
connector
.setPort(Integer.parseInt(config.getProperty("jetty.port")));
server.setConnectors(new Connector[] { connector });
WebAppContext webapp = new WebAppContext();
webapp.setContextPath("/" + jetty_home);// 上下文路径 比如说/demo
// webapp.setResourceBase("./WebRoot");// 你的资源文件所在的路径 一般都在这下面
webapp.setResourceBase(config.getProperty("resourceBase"));
// webapp.setDefaultsDescriptor("./WebRoot/etc/webdefault.xml");
webapp.setDefaultsDescriptor(config.getProperty("webdefault.xml"));
server.setHandler(webapp);
server.addHandler(handlers);
server.start();
server.join();
} catch (InterruptedException e) {
log.error(e.getMessage(), e);
} catch (Exception e) {
log.error(e.getMessage(), e);
}
}
}
项目结构图
[img]http://dl2.iteye.com/upload/attachment/0090/9491/71fce296-eb2d-3103-8239-152413e51ff5.png[/img]
主要代码来自:[url=http://szcs10138456.iteye.com/blog/1011072]jetty的使用[/url],在其基础上增加用户访问记录的[url=http://wiki.eclipse.org/Jetty/Tutorial/RequestLog]代码[/url]
相关阅读:
[url=http://wiki.eclipse.org/Jetty/]Jetty Documentation[/url]
[url=http://www.ibm.com/developerworks/cn/web/wa-lo-jetty/]实站Jetty[/url]
[url=http://www.ibm.com/developerworks/cn/java/j-lo-jetty/]Jetty 的工作原理以及与 Tomcat 的比较[/url]
[url=http://yangzc106.iteye.com/blog/1030438]jetty嵌入式配置总结[/url]