Osgi series-develop one of the server-side Web applications: Implement osgi series through servlet-develop bundle using eclipse

Source: Internet
Author: User

How to Develop Web applications under the osgi framework? This is a problem. There are two ways to run a web application under the osgi framework: the first is to use a Web container as a bundle and then run it in the osgi framework. Second, place the osgi framework in a Web container. This seriesArticleThis is based on the first running mode. At the end of the series, we will introduce the second running mode as appropriate.

 

Step 1: Open eclipse and create a bundle ProjectHelloworldbundle(For detailed process of creating a bundle project using elicpse, refer to osgi series-develop bundle using eclipse ). The directory structure of the helloworldbundle project is as follows:

 

Step 2: OpenManifest. MFFile, switchDependenciesTab, inRequired plug-insAdd two dependency plug-ins:Javax. servletAndOrg. Eclipse. osgi. Services. Javax. servlet provides the servlet API required for servlet development. org. Eclipse. osgi. Services provides the HTTP service used when activator registers the servlet.

Save manifest. MF. The complete anifest. MF content is as follows:

Manifest-version: 1.0Bundle-manifestversion:2Bundle-Name: helloworldbundlebundle-symbolicname: helloworldbundlebundle-version:1.0.0Bundle-Activator: helloworldbundle. activatorimport-package: org. osgi. Framework;Version = "1.3.0"Bundle-requiredexecutionenvironment: JavaSE-1.6Require-Bundle: javax. servlet;Bundle-version = "2.5.0 ",Org. Eclipse. osgi. Services;Bundle-version = "3.3.0"

 

Step 3: CreateHelloservlet. JavaAs our servlet demo. The content of helloservlet. Java is as follows:

 Package Helloworldbundle;  Import  Java. Io. ioexception;  Import  Java. Io. printwriter;  Import  Javax. servlet. http. httpservlet;  Import  Javax. servlet. http. httpservletrequest;  Import  Javax. servlet. http. httpservletresponse;  Import  Javax. servlet. http. httpsession;  Import Org. osgi. Framework. bundlecontext;  Public   Class Helloservlet Extends  Httpservlet {  Private   Static   Final   Long Serialversionuid = 1l ; @ Suppresswarnings ( "UNUSED" )  Private  Bundlecontext context;  Public Helloservlet (bundlecontext context ){  This . Context = Context ;}  Public   Void Doget (httpservletrequest request, httpservletresponse response) Throws  Ioexception {httpsession session = Request. getsession ();  Int  Counter;  If ( Null ! = Session. getattribute ("counter") {Counter = (Integer) Session. getattribute ("counter") + 1 ;}  Else  {Counter = 1 ;} Session. setattribute ( "Counter" , Counter); response. setcharacterencoding ( "GBK" ); Response. setcontenttype ( "Text/html" ); Printwriter out = Response. getwriter (); Out. println ( "<HTML>"); Out. println ( "<Body>" ); Out. println ( "<Head>" ); Out. println ( "<Title> Hello World </title>" ); Out. println ( "</Head>" ); Out. println ( "<Body>" ); Out. println ( "Now time:" + ( New Java. util. Date () + ", counter:" + Counter); Out. println ( "</Body>" ); Out. println ( "</Html>"); Out. Flush (); Out. Close ();}} 

 

 

Step 4: ImplementActivator. JavaTo manage the lifecycle of helloworldbundle. In the start and stop methods of activator, a service listener is added and deleted to listen for HTTP service registration and uninstallation. When the HTTP service is registered, helloservlet is also registered to the HTTP service. When the HTTP service is uninstalled, helloservlet is also uninstalled from the HTTP service. The content of activator. Java is as follows:

 Package  Helloworldbundle;  Import  Org. osgi. Framework. bundleactivator;  Import  Org. osgi. Framework. bundlecontext;  Import  Org. osgi. Framework. serviceevent; Import  Org. osgi. Framework. servicelistener;  Import  Org. osgi. Framework. servicereference;  Import  Org. osgi. Service. http. httpservice;  Public   Class Activator Implements  Bundleactivator, servicelistener {  Private  Bundlecontext CTX;  Private Servicereference Ref;  Public   Void Start (bundlecontext context) Throws  Exception {CTX = Context; context. addservicelistener (  This , "(Objectclass =" + httpservice. Class . Getname () + ")" );}  Public   Void Stop (bundlecontext context) Throws Exception {context. removeservicelistener (  This  );}  Public   Void  Servicechanged (serviceevent event ){  Switch  (Event. GetType ()){  Case  Serviceevent. Registered: registerservlet ();  Break  ;  Case Serviceevent. unregistering: unregisterservlet ();  Break  ;}}  Private   Void  Registerservlet (){  If (Ref = Null  ) {Ref = CTX. getservicereference (httpservice. Class  );  If (Ref! = Null ){  Try  {Httpservice HTTP = CTX. getservice (REF); http. registerservlet ( "/Demo/Hello ", New Helloservlet (CTX ), Null , Null  ); System. Out. println ( "/Demo/Hello already registered" );}  Catch (Exception e) {e. printstacktrace ();}}}}  Private   Void  Unregisterservlet (){  If (Ref! = Null  ){  Try  {Httpservice HTTP = CTX. getservice (REF); http. unregister ( "/Demo/hello" ); System. Out. println ( "/Demo/Hello has been uninstalled"); Ref = Null  ;}  Catch  (Exception e) {e. printstacktrace ();}}}} 

 

Step 5: Select elicpse from the menuRun> run commands..., In the pop-upRun deploymentsFind in the list on the left of the dialog boxOsgi framework, Click the new button:

In the bundles list on the right, selectWorkspaceUnderHellowordbundleProject, and thenTarget PlatformSelect a project:

    • Javax. servlet
    • Org. Eclipse. osgi. Services
    • Org. Eclipse. Equinox. http. servlet
    • Org. Eclipse. Equinox. http. Jetty
    • Org. mortbay. Jetty. Server
    • Org. mortbay. Jetty. util

ClickRunButton. If everything is normal, something we are familiar with will appear.Osgi>Prompt:

 

Step 6: Open your browser and enter the URLHttp: // localhost/demo/HelloIf the following picture is displayed, congratulations, you have succeeded.

 

For writing this article, I would like to thank osgi practices and equinox framework for web development in the osgi environment. Thank you!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.