Embedded jetty with CDI (Weld)

Project overview

src/
├── main
│   ├── java
│   │   └── oct
│   │       └── im
│   │           ├── bean
│   │           │   └── TestBean.java
│   │           ├── Run.java
│   │           └── servlet
│   │               └── TestServlet.java
│   └── resources
│       ├── META-INF
│       │   └── beans.xml
│       └── webapp
│           └── WEB-INF
│               └── web.xml
└── test
    ├── java
            └── resources

Web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="3.0" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
  <display-name>test-jetty-cdi</display-name>
  <listener>
    <listener-class>org.jboss.weld.environment.servlet.Listener</listener-class>
  </listener>
  <servlet>
    <servlet-name>testservlet</servlet-name>
    <servlet-class>oct.im.servlet.TestServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>testservlet</servlet-name>
    <url-pattern>/servlet</url-pattern>
  </servlet-mapping>
</web-app>

Run.java

public static void main(String[] args) throws Exception {

    Server server = new Server(8080);
    WebAppContext webapp = new WebAppContext();

    String webappPath = Run.class.getClassLoader().getResource("webapp").toExternalForm();
    webapp.setDescriptor(webappPath + "/WEB-INF/web.xml");
    webapp.setResourceBase(webappPath);
    webapp.setContextPath("/");

    server.setHandler(webapp);
    server.start();
    server.join();
}

Servlet and Injectable Bean

public class TestServlet extends HttpServlet {

    @Inject
    private TestBean testBean;

    @Override
    public void init(ServletConfig config) throws ServletException {

    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        response.setContentType("text/html");
        response.setStatus(HttpServletResponse.SC_OK);
        response.getWriter().println("<h1>" + "oct.im</h1>");
        if (testBean != null ) {
            response.getWriter().println(testBean.hello());
        }
    }

}

Injectable Bean:

    public class TestBean {

    public String hello() {
        return "Hello from TestBean";
    }
}

Pom.xml

Dependencies in used:

<properties>
  <jetty.version>9.2.0.v20140526</jetty.version>
  <weld.version>2.2.4.Final</weld.version>
</properties>
<dependencies>
  <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.11</version>
    <scope>test</scope>
  </dependency>
  <dependency>
    <groupId>org.jboss.weld.servlet</groupId>
    <artifactId>weld-servlet</artifactId>
    <version>${weld.version}</version>
  </dependency>
  <dependency>
    <groupId>org.eclipse.jetty</groupId>
    <artifactId>jetty-server</artifactId>
    <version>${jetty.version}</version>
  </dependency>
  <dependency>
    <groupId>org.eclipse.jetty</groupId>
    <artifactId>jetty-servlets</artifactId>
    <version>${jetty.version}</version>
  </dependency>
  <dependency>
    <groupId>org.eclipse.jetty</groupId>
    <artifactId>jetty-webapp</artifactId>
    <version>${jetty.version}</version>
  </dependency>
  <dependency>
    <groupId>javax.el</groupId>
    <artifactId>el-api</artifactId>
    <version>2.2</version>
  </dependency>
  <dependency>
    <groupId>javax.servlet.jsp</groupId>
    <artifactId>javax.servlet.jsp-api</artifactId>
    <version>2.3.1</version>
  </dependency>
</dependencies>

Result:

Get localhost:8080/servlet would return:

oct.im

Hello from TestBean

The bean is injected into the Servlet!

Comments !