自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Equinox OSGi服務(wù)器應(yīng)用程序的配置步驟

開發(fā) 后端
在Eclipse里如何配置一個(gè)簡(jiǎn)單的基于Eclipse Equinox OSGi實(shí)現(xiàn)的Web應(yīng)用程序,在它的基礎(chǔ)上可以構(gòu)造更加復(fù)雜的應(yīng)用,本文使用的是Eclipse 3.3.1版本,如果你的Eclipse版本在3.2.0或以上應(yīng)該都可以。

本文介紹在Eclipse里如何配置一個(gè)簡(jiǎn)單的基于Eclipse Equinox OSGi實(shí)現(xiàn)的Web應(yīng)用程序,在它的基礎(chǔ)上可以構(gòu)造更加復(fù)雜的應(yīng)用,本文使用的是Eclipse 3.3.1版本,如果你的Eclipse版本在3.2.0或以上應(yīng)該都可以。

51CTO編輯推薦:OSGi入門與實(shí)踐全攻略

一、支持靜態(tài)頁面和Servlet

1. 創(chuàng)建一個(gè)新的plugin項(xiàng)目, net.bjzhanghao.osgi.test,在向?qū)?**步里選中“This plug-in is target,在下一步的“Plug-in Options”里選中“Generate an activator”。

 


2. 在例子項(xiàng)目的MANIFEST.MF里添加如下依賴項(xiàng)目,這些項(xiàng)目都是Eclipse自帶的:

org.eclipse.equinox.http.jetty
org.eclipse.equinox.http.servlet
org.mortbay.jetty
org.apache.commons.logging
javax.servlet
org.eclipse.equinox.http.registry

3. 在例子項(xiàng)目根目錄下創(chuàng)建一個(gè)放置web文件的目錄,如“web_files”,在這個(gè)目錄下寫一個(gè)簡(jiǎn)單的index.html文件。

4. 為項(xiàng)目建一個(gè)plugin.xml文件,內(nèi)容如下:


 
  alias="/web"
  base-name="/web_files"/>
 

注意,這時(shí)若MANIFEST.MF里提示錯(cuò)誤,只要在Bundle-SymbolicName這一行后面加上“;singleton:=true”即可解決。

5. 現(xiàn)在可以啟動(dòng)這個(gè)應(yīng)用程序了。在Eclipse菜單里選擇“Run->Open Run Dialog...”,在左邊的 “OSGi Framework”項(xiàng)下創(chuàng)建一個(gè)新的啟動(dòng)配置項(xiàng),在右邊先點(diǎn)“Deselect All”清空所有復(fù)選框,然后在Workspace下選中自己的osgi項(xiàng)目,再點(diǎn)“Add Required Bundles”按鈕,Eclipse會(huì)自動(dòng)把所依賴的項(xiàng)目選中。最后按“Debug”按鈕啟動(dòng),內(nèi)嵌的jetty和我們的項(xiàng)目會(huì)一起被啟動(dòng)。

 

6. 打開瀏覽器,輸入“http://localhost/web/index.html”應(yīng)該可以看到index.html里的內(nèi)容。

以上只驗(yàn)證了靜態(tài)頁面,現(xiàn)在來配置一個(gè)servlet看看。

7. 在項(xiàng)目里創(chuàng)建一個(gè)繼承自HttpServlet的類,覆蓋doGet()方法,內(nèi)容是在網(wǎng)頁上打印一些文本。

8. 在項(xiàng)目的plugin.xml里添加下面的內(nèi)容,這些內(nèi)容指定了servlet的訪問路徑和實(shí)現(xiàn)類:


  alias="/exampleServlet"
  class="net.bjzhanghao.osgi.example.servlet.ExampleServlet"/>

9. 重新啟動(dòng)項(xiàng)目,在瀏覽器里輸入“http://localhost/exampleServlet”,應(yīng)該可以看到servlet的輸出。

二、支持JSP頁面

10. 在index.html所在目錄下創(chuàng)建一個(gè)簡(jiǎn)單的jsp文件index.jsp

11. 打開項(xiàng)目的MANIFEST.MF文件,添加如下項(xiàng)目依賴:

org.eclipse.equinox.jsp.jasper,
org.apache.jasper,
org.eclipse.equinox.jsp.jasper.registry,
javax.servlet.jsp,
org.apache.commons.el,
org.eclipse.equinox.http.helper,
org.eclipse.osgi,
org.eclipse.osgi.services

其中org.eclipse.equinox.http.helper需要從cvs里下載得到(目前是在/cvsroot/eclipse下的 equinox-incubator目錄里,以后可能會(huì)直接放到/cvsroot/eclipse下)。

12. 修改Activator,目的是注冊(cè)一個(gè)處理擴(kuò)展名為.jsp類型的servlet,感覺這一步以后應(yīng)該有更簡(jiǎn)單的方法,例如通過擴(kuò)展點(diǎn)。

public class Activator implements BundleActivator {

private ServiceTracker httpServiceTracker;

String jspContext = "/jsps";
String jspFolder = "/web_files";

public void start(BundleContext context) throws Exception {
httpServiceTracker = new HttpServiceTracker(context);
httpServiceTracker.open();
}

public void stop(BundleContext context) throws Exception {
httpServiceTracker.open();
}

private class HttpServiceTracker extends ServiceTracker {

public HttpServiceTracker(BundleContext context) {
super(context, HttpService.class.getName(), null);
}

public Object addingService(ServiceReference reference) {
final HttpService httpService = (HttpService) context
.getService(reference);
try {
HttpContext commonContext = new BundleEntryHttpContext(context
.getBundle(), jspFolder);
httpService.registerResources(jspContext, "/", commonContext);

Servlet adaptedJspServlet = new ContextPathServletAdaptor(
new JspServlet(context.getBundle(), jspFolder),
jspContext);
httpService.registerServlet(jspContext + "/*.jsp",
adaptedJspServlet, null, commonContext);
} catch (Exception e) {
e.printStackTrace();
}
return httpService;
}

public void removedService(ServiceReference reference, Object service) {
final HttpService httpService = (HttpService) service;
httpService.unregister(jspContext);
httpService.unregister(jspContext + "/*.jsp");
super.removedService(reference, service);
}
}
}

13. 打開Debug對(duì)話框,選中workspace里的例子osgi項(xiàng)目和org.eclipse.equinox.http.helper項(xiàng)目,再按“Add Required Bundles”按鈕,然后啟動(dòng)程序。

14. 在瀏覽器里輸入“http://localhost/jsps/index.jsp”,應(yīng)該可以看到j(luò)sp輸出。

您正在閱讀:Equinox OSGi服務(wù)器應(yīng)用程序的配置步驟

【編輯推薦】

  1. Eclipse、Equinox和OSGi
  2. Equinox OSGi系列之一 Equinox入門
  3. 探索Eclipse的OSGi控制臺(tái)
責(zé)任編輯:張攀 來源: 博客園
相關(guān)推薦

2009-06-18 10:03:57

EquinoxOSGi應(yīng)用服務(wù)器

2009-11-18 09:48:38

Windows PHP

2009-06-01 11:28:48

EquinoxOSGi入門

2009-06-25 17:08:14

2017-11-10 08:58:49

Web服務(wù)器應(yīng)用程序

2011-09-06 10:58:10

服務(wù)器應(yīng)用程序虛擬化

2010-04-16 13:32:28

Win2008 R2

2009-10-15 15:12:39

Equinox服務(wù)器端Equinox

2009-09-29 14:20:05

OSGiContactDAO

2011-07-22 18:44:45

iPhone HTTPS 服務(wù)器

2009-10-22 11:03:20

OSGi Web應(yīng)用程

2018-03-12 09:13:12

應(yīng)用程序服務(wù)器部署

2013-12-11 09:56:10

存儲(chǔ)加速器

2011-11-16 11:30:08

虛擬化虛擬服務(wù)器虛擬服務(wù)器備份

2019-05-14 09:39:07

Web服務(wù)器Web容器應(yīng)用程序服務(wù)器

2009-10-19 14:14:19

OSGi Web應(yīng)用

2010-07-01 14:22:02

配置FTP服務(wù)器

2024-01-03 11:44:26

開發(fā)云服務(wù)

2011-07-26 16:43:59

iPhone Web 服務(wù)器

2018-11-23 08:54:27

服務(wù)器程序監(jiān)控
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)