说起 Jetty,没有几个人知道,而实际上由于 Jetty 的 License 非常开放,采用 Jetty 的商业产品是非常多的。Resin 据说也非常棒,但是由于 License 的原因我们不可能使用 Resin。
对 Jetty 感兴趣的朋友可以到这里看看:
http://www.mortbay.com/mortbay/powered.html
Jetty Powered
As a small, fast, embeddable web server and servlet container Jetty is used by a plethora of both commercial and open source projects.
We've listed just a few here to illustrate Jetty's versatility and the diversity of it's community. Feel free to contact us if you would like your product or project added here.
在这些使用 Jetty 的产品中不仅包括 JBoss、JOnAS 这样开源的 AppServer,还包括 WebLogic Business Connect 和 IBM Tivoli NetView 这样的商业产品。
dlee 写道
现在大家对于 Jetty 的批判全部都集中在 Jetty 的性能方面。但是对于一个产品的判断性能仅仅是一个方面,当然是一个非常重要的方面,但是也不要以偏概全。
......
我举这个例子是为了说明什么?
我仍然要说 Jetty 是一个非常好的 Web Container。因为它的设计简练而清晰(Jetty 的代码要比 Tomcat 的代码简单和清晰的多),它非常容易被嵌入到其它产品之中。它用最少的代码提供了我们常用的几乎所有 Web Container 的功能。
Server server = new Server(8080);
Context root = new Context(server,"/",Context.SESSIONS);
root.addServlet(new ServletHolder(new HelloServlet("Ciao")), "/*");
server.start();
这样就等于配置了一个servlet mapping, 感觉是很神奇, 不过不用web.xml倒不是我想要的,我毕竟要兼顾不同web容器的兼容性。把Embedding Jetty里面的各种示例看了个遍,发现都没直接用web.xml的。
Eclipse Workbench中介绍了几个jetty for eclipse插件,试用了一下,感觉这个就不错了,简单可用:http://docs.codehaus.org/display/JETTY/Web+Tooling+Support
很多人推荐的jettylaucher反倒是没用起来, 似乎是还不支持最新的jetty6.1。
Configuration介绍了jetty.xml的配置,就是启动jetty指定的那个配置参数。
jetty自身的那些配置我倒一开始没看,一心在想怎样移植webapp到jetty这来,于是还真找到了,就在Contexts and Web Applications子章节里面有说明,分别打开Web Application Deployer (static deploy)和 Context Deployer (hot deploy!),发现分别说的是静态部署和动态部署。静态部署就是 jetty.home/webapps/下面的应用可以启动jetty时一次都给部署,跟tomcat.home/webapps也是一样的;如果能够直接指定contextpath和webapppath就好了,动态部署估计就是我想要的了,如果仔细看一下
引用
Typically a ContextDeployer is defined in a jetty.xml file:
The ContextDeployer will scan the configurationDir directory at intervals of scanInterval seconds for xml descriptors that define contexts. Any contexts found are deployed to the passed contexts reference to a HandlerContainer (this is normally an instance of ContextHandlerCollection).
The deployment descriptors are in jetty xml format and are define and configure individual contexts. A minimal example is
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" "http://jetty.mortbay.org/configure.dtd">
<Configure class="org.mortbay.jetty.webapp.WebAppContext">
<Set name="contextPath">/test</Set>
<Set name="war"><SystemProperty name="jetty.home" default="."/>/webapps/test</Set>
</Configure>
This example creates an instance of org.mortbay.jetty.webapp.WebAppContext and sets the contextPath to be "/test" and the resourceBase to be "$jetty.home/webapps/test". Because the context used is a standard web application context, when started it will inspect the resourceBase for a WEB-INF/web.xml for further configuration.
The ContextDeployer is added to the server as a LifeCycle. This simply means that the deployer will be started and stopped with the server. Ie when server.start() is called, then start will also be called on the deployer.
public static void main(String[] args) throws Exception {
Server server = new Server();
XmlConfiguration configuration = new XmlConfiguration(
new FileInputStream(
"xx/demo/jetty-web.xml")); //指定自定义的jetty.xml路径
configuration.configure(server);
server.start();
}