Configuration steps for the Equinox OSGi Server application (JSP page support)

Source: Internet
Author: User

This article describes how to configure a simple Web application based on Eclipse Equinox OSGi implementation in Eclipse, based on which you can construct more complex applications, this article uses the Eclipse 3.3.1 version, If your eclipse version is 3.2.0 or above, you should be able to.

51CTO Editor's recommendation: Introduction and practice of OSGi all about

I. Support for static pages and Servlets

1. Create a new plugin project, Net.bjzhanghao.osgi.test, in the first step of the wizard, select "This plug-in is target, select" Generate an in the next "plug-in Options" Activator ".


2. Add the following dependencies to the MANIFEST.MF of the example project, all of which are from eclipse:

org.eclipse.equinox.http.jettyorg.eclipse.equinox.http.servletorg.mortbay.jettyorg.apache.commons.loggingjavax.servletorg . eclipse.equinox.http.registry

3. In the example project root directory, create a directory where Web files are placed, such as "Web_files", and write a simple index.html file in this directory.

4. Build a Plugin.xml file for the project, which reads as follows:



Alias= "/web"
Base-name= "/web_files"/>

Note that if the MANIFEST.MF prompt error, as long as the bundle-symbolicname this line after adding "; Singleton:=true" can be resolved.

5. You can now start the application. In the Eclipse menu, select "Run->open Run Dialog ..." To create a new startup configuration item under the "OSGi Framework" item on the left, and click "Deselect all" to clear all the checkboxes on the right. Then select your OSGi project under workspace, then click the "Add Required Bundles" button, and Eclipse will automatically select the item you depend on. Finally press the "Debug" button to start, the embedded jetty and our project will be started together.

6. Open the browser, enter "http://localhost/web/index.html" should be able to see the contents of index.html.

The above only validates the static page, now to configure a servlet to look at.

7. Create a class in the project that inherits from HttpServlet, overriding the Doget () method, which prints some text on the Web page.

8. In the plugin.xml of the project, add the following content that specifies the access path and implementation class for the servlet:


Alias= "/exampleservlet"
class= "Net.bjzhanghao.osgi.example.servlet.ExampleServlet"/>

9. Restart the project, enter "Http://localhost/exampleServlet" in the browser, you should be able to see the output of the servlet.

Second, support JSP page

10. Create a simple JSP file in the directory where the index.html is located index.jsp

11. Open the project's MANIFEST.MF file 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

Where Org.eclipse.equinox.http.helper needs to be downloaded from CVS (currently in the Equinox-incubator directory under/cvsroot/eclipse and may be placed directly in the/cvsroot/ Eclipse).

12. Modify the activator in order to register a servlet with the. jsp extension, and feel that there should be a simpler way to do so, 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); Httpser Vicetracker.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. Getservi CE (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) ser Vice Httpservice.unregister (Jspcontext); Httpservice.unregister (Jspcontext + "/*.jsp"); Super.removedservice (reference, service); } } }

13. Open the Debug dialog box, select the example OSGi project and Org.eclipse.equinox.http.helper project in workspace, then press the "ADD Required Bundles" button and start the program.

14. Enter "Http://localhost/jsps/index.jsp" in the browser, you should see the JSP output

Configuration steps for the Equinox OSGi Server application (JSP page support)

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.