Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use servlet with OSGi

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

http://localhost:8080/testservice. 

The service generate HTML response in a servlet.

I have 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>");
  }
}

The tool I need to use:

  1. maven to create the project

  2. Fuse ESB karaf as OSGi container

The question is that I do not know how to use Maven to create and implement such web service, like:

  • how to specify webapp/web.xml

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

  • how to register service: implement BundlActivator or configure Spring xml file

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

like image 258
Li' Avatar asked May 01 '13 06:05

Li'


People also ask

Why do we use servlets in AEM?

A Servlet is a class used to extend the capabilities of servers that host applications accessed by means of a request-response programming model.

What is OSGi Java container?

OSGi is a Java framework for developing and deploying modular software programs and libraries. Each bundle is a tightly coupled, dynamically loadable collection of classes, jars, and configuration files that explicitly declare their external dependencies (if any).

How are servlets registered in AEM?

Designate — Generate a Designate element in the Meta Type Resource for an ObjectClassDefinition using the annotated Declarative Services component. Define a Designate to the servlet to the ObjectClassDefinition. Now the servlet is registered with configured resource types, selectors, and extensions.


1 Answers

If you use 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 descriptor with 'Apache Felix 4 with Web Console and Gogo', just add the Apache Felix Http whiteboard bundle and you're good to go. You can find your servlet at http://localhost:8080/hello

How it works. The @Component annotation makes your class a service (a Servlet service in this case due to the provide attribute). This is registered with the service property 'alias'. The Apache Felix Http Whiteboard bundle picks up these services and registers them as servlets. I do not think it can get any simpler than this.

like image 109
Peter Kriens Avatar answered Sep 29 '22 20:09

Peter Kriens