====== OSGi Web Application ======
OSGi has support for web applications included for a long time in form of Java Servlet support.
===== Maven =====
The maven-bundle-plugin does most of the work for getting the manifest right.
javax.servlet
javax.servlet-api
3.0.1
provided
org.apache.felix
maven-bundle-plugin
2.3.5
bundle-manifest
process-classes
manifest
jar
bundle
war
WEB-INF/lib
*;scope=compile
true
maven-war-plugin
org.apache.maven.plugins
2.4
false
${project.build.outputDirectory}/META-INF/MANIFEST.MF
The project specific manifest entries are extracted into an extra properties file:
Bundle-ClassPath: .,WEB-INF/classes
Web-ContextPath: /application_url_part
It is important to add the root of the WAR file to the bundle classpath.
===== Servlet =====
An extremely simple Servlet:
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet(displayName="RPG Next Gen OSGi Simple Servlet" , value="/*")
public class SimpleServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter writer = response.getWriter();
writer.println("Enter the matrix ...");
}
}
No ''web.xml'' file is needed for this simple setup.
===== Apache Karaf =====
For this example to work in Apache Karaf only the ''war'' feature is needed.
feature:install war
{{tag>osgi devel java}}