Create and run simple servlet

1. Create a new eclipse plug-in project org.sevenhats.sampleservlet and set SevenHats platform as your Target Platform.
2. Edit META-INF/MANIFEST.MF 

Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: Sample SevenHats servlet
Bundle-SymbolicName: org.sevenhats.sampleservlet
Bundle-Version: 1.0.0
Import-Package:
javax.servlet;version="2.4.0",
javax.servlet.http;version="2.4.0",
org.osgi.framework;version="1.4.0",
org.sevenhats.web.api,
org.springframework.beans;version="2.5.2",
org.springframework.context;version="2.5.2",
org.springframework.web.filter;version="2.5.2"

3. Create a sample servlet

package org.sevenhats.sampleservlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class SampleServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        resp.getWriter().print("Sample Servlet");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        this.doGet(req, resp);
    }

}

4. Configure servlet provider for SampleServlet.You can use org.sevenhats.web.beans.ServletProviderBean class and configure it using Spring context.

Properties for  ServletProviderBean

  • name - Servlet name
  • mappingPattern - Servlet configuration's url-pattern analogy: matches a set of URLs for servlet
  • servlet - Your servlet
  • applicationContext - Spring application context
  • bundleContext - Bundle context
  • initParameters  - web.xml'scontext-params parameters.

Or you can write your own implementation of org.sevenhats.web.api.ServletProvider interface.

5. Create META-INF/spring/sampleservlet-context.xml and META-INF/spring/sampleservlet-context-osgi.xml files

sampleservlet-context.xml:

   

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:osgi="http://www.springframework.org/schema/osgi"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
    http://www.springframework.org/schema/osgi http://www.springframework.org/schema/osgi/spring-osgi.xsd">

    <bean id="sampleServletProvider"
        class="org.sevenhats.web.beans.ServletProviderBean">
        <property name="bundleContext" ref="bundleContext" />
        <property name="mappingPattern" value="/sample" />
        <property name="name" value="SampleServlet" />
        <property name="servlet" ref="sampleServlet" />
    </bean>

    <bean id="sampleServlet"
        class="org.sevenhats.sampleservlet.SampleServlet" />
</beans>

sampleservlet-context-osgi.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:osgi="http://www.springframework.org/schema/osgi"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
                      http://www.springframework.org/schema/osgi http://www.springframework.org/schema/osgi/spring-osgi.xsd">

    <osgi:service ref="sampleServletProvider"
        interface="org.sevenhats.web.api.ServletProvider"
        context-class-loader="service-provider" />
</beans>

6. Run application as a Seven Hats project.
7. Check out servlet using http://localhost/sample.

Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.