class="java">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.Server;
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";// 这个就是你的项目发布时候的名字
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.start();
server.join();
} catch (InterruptedException e) {
log.error(e.getMessage(), e);
} catch (Exception e) {
log.error(e.getMessage(), e);
}
}
}
项目结构图
以上代码来自:jetty的使用
相关阅读:实站Jetty
- jettydemo.zip (11.6 KB)
- 描述: 完整代码(Maven项目)
- 下载次数: 1
- 描述: 项目结构图
- 大小: 28.8 KB