How to use a servlet with osgi

I want to create and deploy a web service in an OSGi container. For example, publish the service at:

http://localhost:8080/testservice. 

The service generates an HTML response in the servlet.

I searched a lot and got:

 public class HelloWorldServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("<html>"); out.println("<head>"); out.println("<title>Hola</title>"); out.println("</head>"); out.println("<body bgcolor=\"white\">"); out.println("</body>"); out.println("</html>"); } 

}

Tool I need to use:

  • maven to create a project

  • ESB karaf fuse as an OSGi container

The question is, I donโ€™t know how to use Maven to create and implement such a web service, for example:

how to specify webapp / web.xml

how to specify pom.xml: dependencies, package type, plugin

how to register a service: implement BundlActivator or configure spring xml file

Can anyone help me with this? Is there a detailed tutorial for beginners?

+8
java html maven servlets osgi
source share
6 answers

If you are using bndtools, create a Declarative Services project and add this annotation to your servlet:

  @Component(provide = Servlet.class, properties = {"alias=/hello"}) public class HelloWorldServlet extends HttpServlet { ... } 

Then create a bnd run handle with "Apache Felix 4 with web console and Gogo", just add the Apache Felix Http board package, and you will be fine. You can find your servlet at http://localhost:8080/hello

How it works. The @Component annotation makes your class a service (the Servlet service in this case is due to the provisioning attribute). This is registered in the service alias property. The Apache Felix Http Whiteboard bundle picks up these services and registers them as servlets. I donโ€™t think it could be easier than that.

+4
source share

Check this maybe maybe help you create a servlet that accesses the OSGi service

+3
source share

I would like to continue the answer of Peter Criens . With @Component annotations available in the OSGi specification, an example might look like this:

 @Component(service = Servlet.class, property = { "osgi.http.whiteboard.servlet.pattern = /hello" }) public class HelloWorldServlet extends HttpServlet { ... } 

The @Component annotation @Component imported from org.osgi.service.component and the property that defines the deployed service has changed its name to service .

Despite its name, property can contain several properties, for example

 @Component(service = ..., property = { "a=b", "c=d" }) 

or you can use properties to specify one or more property files, for example like this:

 @Component(service = ..., properties = { "OSGI-INF/servlet.properties" } ) 

Above has been tested with the HttpService which comes with Apache Felix. The Apache Felix HTTP Service documentation can be found here: http://felix.apache.org/documentation/subprojects/apache-felix-http-service.html.

+3
source share

You can find the following tutorial: http://www.javabeat.net/2011/11/writing-an-osgi-web-application/ . It is based on the second chapter of Enterprise OSGi in action . Chapter Eight also discusses how to use build tools like maven to get the package structure right, and http://coding.alasdair.info/2011/01/creating-web-application-bundle-using.html also has a really Useful maven instructions.

At a high level, your best route should probably use something like Apache Aries or Eclipse Gemini so that you can run the WAB (website). WAB is structured in much the same way as WAR, except that the manifest has OSGi metadata. Your servlet class will be identical to the case other than OSGi. The framework will handle the detection and launch of your servlet.

+1
source share

To answer your question, since Karaf (FUSE ESB) uses Pax Web, since by default the Web-Container considers Pax Web for more details. details on how this works and is probably best for you in over 100 Pax Web integration tests to give you an idea of โ€‹โ€‹how to use it. There are also samples to show you how to use either std. Http-Service, via expander board or as WAR / WAB.

0
source share

I think you need a servlet bridge to access the service. Your service should be implemented as an OSGI package; the servlet bridge must have an integrated OSGI infrastructure. Follow this sample: http://vbashur.blogspot.kr/2014/07/osgi-servlet-bridge-sample.html

0
source share

All Articles