Configuration steps for Equinox osgi server applications (JSP pages supported)

Source: Internet
Author: User

This article describes how to configure a simple eclipse-based Web Application Based on Eclipse equinox osgi and how to 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.

51cto: Introduction to osgi and full introduction to practice

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:

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

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:

Alias = "/exampleservlet"
Class = "net. bjzhanghao. osgi. example. servlet. exampleservlet"/>

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 = (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. 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.

You are reading the configuration steps of equinox osgi server application.

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.