Differences

This shows you the differences between two versions of the page.

Link to this comparison view

osgi_web_application [2014/03/18 21:39]
osgi_web_application [2021/04/05 11:23] (current)
Line 1: Line 1:
 +====== 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.
 +
 +<sxh xml>
 +<dependencies>
 + <dependency>
 + <groupId>javax.servlet</groupId>
 + <artifactId>javax.servlet-api</artifactId>
 + <version>3.0.1</version>
 + <scope>provided</scope>
 + </dependency>
 + </dependencies>
 +
 + <build>
 + <plugins>
 + <plugin>
 + <groupId>org.apache.felix</groupId>
 + <artifactId>maven-bundle-plugin</artifactId>
 + <version>2.3.5</version>
 + <executions>
 + <execution>
 + <id>bundle-manifest</id>
 + <phase>process-classes</phase>
 + <goals>
 + <goal>manifest</goal>
 + </goals>
 + </execution>
 + </executions>
 + <configuration>
 + <supportedProjectTypes>
 + <supportedProjectType>jar</supportedProjectType>
 + <supportedProjectType>bundle</supportedProjectType>
 + <supportedProjectType>war</supportedProjectType>
 + </supportedProjectTypes>
 + <instructions>
 + <Embed-Directory>WEB-INF/lib</Embed-Directory>
 + <Embed-Dependency>*;scope=compile</Embed-Dependency>
 + <Embed-Transitive>true</Embed-Transitive>
 + </instructions>
 + </configuration>
 + </plugin>
 + <plugin>
 + <!-- Need to use this plugin to build war files -->
 + <artifactId>maven-war-plugin</artifactId>
 + <groupId>org.apache.maven.plugins</groupId>
 +
 + <version>2.4</version>
 + <configuration>
 + <!-- We don't always have a web.xml -->
 + <failOnMissingWebXml>false</failOnMissingWebXml>
 +
 + <archive>
 + <manifestFile>${project.build.outputDirectory}/META-INF/MANIFEST.MF</manifestFile>
 + </archive>
 + </configuration>
 + </plugin>
 + </plugins>
 + </build>
 +</sxh>
 +
 +The project specific manifest entries are extracted into an extra properties file:
 +
 +<sxh title:osgi.properties>
 +Bundle-ClassPath: .,WEB-INF/classes
 +Web-ContextPath: /application_url_part
 +</sxh>
 +
 +It is important to add the root of the WAR file to the bundle classpath.
 +
 +===== Servlet =====
 +An extremely simple Servlet:
 +
 +<sxh java>
 +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 ...");
 + }
 +}
 +</sxh>
 +
 +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}}