This article describes how to configure a simple Web Application Based on Eclipse equinox osgi in eclipse.ProgramYou can construct more complex applications based on it. This article uses eclipse 3.3.1. If your eclipse version is 3.2.0 or later, you can use it.
1. Support for static pages and servlets
1. create a new plugin project, net. bjzhanghao. osgi. test, select "this plug-in is target" in the first step of the wizard, and select "generate an activator" in "plug-in Options" in the next step ".
2. Add the following dependency projects to manifest. MF of the example project. These projects are all built-in to eclipse:
Org. Eclipse. Equinox. http. jettyorg. Eclipse. Equinox. http. servletorg. mortbay. jettyorg. Apache. commons. loggingjavax. servletorg. Eclipse. Equinox. http. Registry
3. Create a webfile directory under the root directory of the project, for example, web_filesfolder. Write a simple index.html file in this directory.
4. Create a plugin. xml file for the project. The content is as follows:
plugin > extension point =" org. eclipse. equinox. HTTP. registry. resources " > resource alias ="/Web" base-name ="/web_files " /> extension > plugin >
Note: if an error is prompted in manifest. MF, you only need to add "; singleton: = true" after the bundle-symbolicname line.
5. Now you can start this application. In the eclipse menu, select "Run-> open run dialog... ", create a new startup configuration item under the" osgi framework "item on the left, click" deselect all "on the right to clear all check boxes, and then select your osgi project under workspace, click "add required bundles". Eclipse automatically selects the dependent project. Finally, press the "debug" button to start. The embedded jetty and our project will be started together.
6. Open your browser and enter "http: // localhost/web/index.html#" to check the content in index.html.
The above only verifies the static page. Now let's configure a servlet.
7. Create a class in the project that inherits from httpservlet and overwrite the doget () method. The content is to print some text on the webpage.
8. Add the following content to the project plugin. XML, which specifies the servlet access path and implementation class:
<ExtensionPoint= "Org. Eclipse. Equinox. http. Registry. servlets"><ServletAlias= "/Exampleservlet"Class= "Net. bjzhanghao. osgi. example. servlet. exampleservlet"/></Extension>
9. Restart the project and enter "http: // localhost/exampleservlet" in the browser. The servlet output is displayed.
Ii. Support JSP pages
10. Create a simple JSP file index. jsp in the directory where index.html is located
11. Open the manifest. MF file of the project and add the following project dependencies:
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 needs to be downloaded from CVS (currently in the Equinox-incubator directory under/cvsroot/eclipse, and may be directly put under/cvsroot/eclipse in the future ).
12. Modify the activator to register a servlet with a processing extension of the. jsp type. I feel that there should be simpler methods after this step, such as through extension points.
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) 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) service; httpservice. unregister (jspcontext); httpservice. unregister (jspcontext + "/*. Jsp" ); Super . Removedservice (reference, service );}}}
13. Open the debug dialog box, select the sample osgi project in workspace and the org. Eclipse. Equinox. http. helper project, click "add required bundles", and start the program.
14. Enter "http: // localhost/JSPs/index. jsp" in the browser to view the JSP output.
Example ProjectSource codeDownload (link ).
Reference link:
- Embedding an HTTP server in equinox
- Writing a bundle-based server application
- Osgi based JSP support