eclipse下Jetty Plugin的开发应用
Jetty做为一个轻量级的J2EE Web application server,它不仅小巧,而且性能也比较稳定,效率也挺高,现在也越来越得到广泛的应用。特别是eclipse平台集成了Jetty Plugin后,更是对RCP整合Web Server开发提供了极大的方便。一个非常典型的应用应该就是help帮助系统了,大家可以参看eclipse自已的help系统,从3.3后它就是基于Jetty的,现在的Jetty版本是5.1。
在这里不得不先叉开一个话题,就是servlet-bridge桥的概念。Jetty只是一个单纯的web server服务器,eclipse为了整合框架的考虑,提出了servlet-bridge的概念,对Jetty进行了封装,提供了基于Extension point的servlet注册机制。用户只需要在plugin.xml文件中对相应的Extension point进行扩展,就可以很方便将一个servlet注册到web server上。也就是说,标准的J2EE应用,我们需要一个web.xml文件对所有的servlet,context-param等等进行配置说明;现在这些都基于eclipse http registry所提供的Extension point进行注册,不再需要web.xml文件。
下面,我们看一个plugin.xml的例子。
<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.0"?>
<plugin>
<extension
point="org.eclipse.equinox.http.registry.httpcontexts">
<httpcontext
id="myapp">
<resource-mapping
path="/myapp">
</resource-mapping>
</httpcontext>
</extension>
<extension
point="org.eclipse.equinox.http.registry.resources">
<resource
alias="/"
base-name="/"
httpcontextId="myapp">
</resource>
<serviceSelector
filter="(other.info=org.myapp.jetty.demo)">
</serviceSelector>
</extension>
<extension
point="org.eclipse.equinox.http.registry.servlets">
<servlet
alias="/myfirstservlet"
+ webappName ); //$NON-NLS-1$ //$NON-NLS-2$ dict.put( "other.info", pluginID ); //$NON-NLS-1$ // Startup Jetty web server JettyConfigurator.startServer( webappName, dict ); ensureBundleStarted( "org.eclipse.equinox.http.registry" ); //$NON-NLS-1$
停止一个Web application,一句代码就够了:
JettyConfigurator.stopServer( webappName );