This example show how to register a servlet in the Karaf HTTP Service.
It uses different approaches:
@WebServlet
annotation using the Karaf annotation scanner@WebServlet
Servlet 3.0 annotation to register.The build uses Apache Maven. Simply use:
mvn clean install
On a running Karaf instance, you register the example features repository with:
karaf@root()> feature:repo-add mvn:org.apache.karaf.examples/karaf-servlet-example-features/LATEST/xml
As requirement, you need to install a HTTP service provider (for instance felix-http
feature or http
(Pax Web) feature). So either:
karaf@root()> feature:install http karaf@root()> feature:install http-whiteboard
or
karaf@root()> feature:install felix-http
The karaf-servlet-example-annotation
feature installs the required features (HTTP service) and register the servlet using Servlet 3.0 annotation:
karaf@root()> feature:install karaf-servlet-example-annotation
The karaf-servlet-example-registration
feature installs the required features (HTTP service) and register the servlet by hand in the Karaf HTTP service:
karaf@root()> feature:install karaf-servlet-example-registration
The karaf-servlet-example-blueprint
feature installs the required features (HTTP service & blueprint) and register the servlet using a Servlet service:
karaf@root()> feature:install karaf-servlet-example-blueprint
The karaf-servlet-example-scr
feature installs the required features (HTTP service & SCR) and register the servlet using a Servlet service:
karaf@root()> feature:install karaf-servlet-example-scr
Whatever feature you use, you can access the servlet on the following URL:
[http://localhost:8181/servlet-example]
You can also find a upload servlet example using multipart data.
You can install it with:
karaf@root()> feature:install karaf-servlet-example-upload
Then, you can use curl
to upload data via this servlet:
curl --progress-bar -v -k -F file=/my/file http://127.0.0.1:8181/upload-example