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"
class="org.myapp.jetty.demo.servlet.MyFirstServlet"
httpcontextId="myapp"
load-on-startup="true">
</servlet>
<servlet
alias="/*.jsp"
class="org.eclipse.equinox.jsp.jasper.registry.JSPFactory:/"
httpcontextId="myapp">
</servlet>
<serviceSelector
filter="(other.info=org.myapp.jetty.demo)">
</serviceSelector>
</extension>
</plugin>
這個文件不算復雜,簡單解釋一下:
1) Extension point - "org.eclipse.equinox.http.registry.httpcontexts"
這個類似于ServletContext,一般一個web application指定一個,ID是唯一的。 resource-mapping可以指定資源根目錄。
2) Extension point - "org.eclipse.equinox.http.registry.resources"
是資源目錄和httpcontext間的影射關系。
3) Extension point - "org.eclipse.equinox.http.registry.servlets"
這個很關鍵,就是用這個來注冊servlet的。
其中有兩個地方要注意,
<servlet alias="/*.jsp" class="org.eclipse.equinox.jsp.jasper.registry.JSPFactory:/" httpcontextId="myapp"></servlet>
這個是注冊所有jsp的mapping,一定要寫。
<serviceSelector
filter="(other.info=org.myapp.jetty.demo)">
</serviceSelector>
這一段最好也要保留,不然要多個web app的情況下,可能會產生沖突。
有什么不情楚的,大家可以查看eclipse的幫助。
很可惜,eclipse基本上只提供了這幾個Extension point,現在只提供對servlet的支持,暫時還無法支持context-param, filter, listener等等其它的元素。
已經有相應的bug,但似乎不在eclipse的下一步開發計劃中。
https://bugs.eclipse.org/bugs/show_bug.cgi?id=199748
而且在實際的開發過程中還發現兩個問題:
1) 當調用ServletContext.getRealPath方法獲取絕對路徑時,返回NULL值。
2) 當在JSP中調用request.getServletPath方法,也是返回NULL值。
所以需要找到別的方法去處理。
在Jetty Server上啟動一個Web application,代碼非常簡單:
Java代碼:
Dictionary dict = new Hashtable( );
// configure the port
dict.put( "http.port", port ); //$NON-NLS-1$
// configure the host
dict.put( "http.host", host ); //$NON-NLS-1$
// set the base URL
dict.put( "context.path", "/" + 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,一句代碼就夠了:
Java代碼
JettyConfigurator.stopServer( webappName );
附件中是我寫的一個示例plugin,包含了所有基本的功能。包括啟動/停止一個web application,注冊servlet,JSP文件示例等等。
大家可以很方便的應用到自已開發的RCP應用中去。
轉載自:http://cyfgod.iteye.com/blog/286893
?
?
轉載于:https://blog.51cto.com/3237526/1611260
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的Eclipse jetty和plugin 的结合使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 算法系列之图--DFS
- 下一篇: DWZ使用笔记