JBoss.orgCommunity Documentation

Chapter 3. CDI + Servlet: Helloworld quickstart

3.1. Deploying the Helloworld example using Eclipse
3.2. The helloworld example in depth

This quickstart shows you how to deploy a simple servlet to JBoss AS 7. The business logic is encapsulated in a service, which is provided as a CDI bean, and injected into the Servlet.

Contexts and Dependency Injection for Java EE

CDI is a new specification in Java EE 6, inspired by JBoss Seam and Google Guice, and also drawing on lessons learned from frameworks such as Spring. It allows application developers to concentrate on developing their application logic by providing the ability to wire services together, and abstract out orthogonal concerns, all in a type safe manner.

Switch to the quickstarts/helloworld directory and instruct Maven to build and deploy the application:

mvn package jboss-as:deploy

The quickstart uses a Maven plugin to deploy the application. The plugin requires JBoss AS to be running (you can find out how to start the server in Section 2.1, “Installing and starting JBoss AS 7 on Linux, Unix or Mac OS X” or Section 2.2, “Installing and starting JBoss AS on Windows”).

Now, check if the application has deployed properly by clicking http://localhost:8080/jboss-as-helloworld. If you see a "Hello World" message it's all working!


Should you wish to undeploy the quickstart, or redeploy after making some changes, it's pretty easy:

  • mvn jboss-as:deploy - deploy any changes to the application to the application server

  • mvn jboss-as:undeploy - undeploy the example from JBoss AS

You may choose to deploy the example using Eclipse. You'll need to have JBoss AS 7 started in Eclipse (as described in Section 2.3, “Starting JBoss AS from Eclipse with JBoss Tools”) and to have imported the quickstarts into Eclipse (as described in Section 2.4, “Importing the quickstarts into Eclipse”).

With the quickstarts imported, you can deploy the example by right clicking on the jboss-as-helloworld project, and choosing Run As -> Run On Server:

Make sure the JBoss AS 7 server is selected, and hit Finish:

You should see JBoss AS 7 start up (unless you already started it in Section 2.3, “Starting JBoss AS from Eclipse with JBoss Tools”) and the application deploy in the Console log:

It's time to pull the covers back and dive into the internals of the example application.

The helloworld is very simple - all it does is print "Hello World" onto a web page.

The helloworld example is comprised a servlet and a CDI bean. We also include an empty beans.xml file, which tells JBoss AS 7 to look for beans in this application and to activate the CDI. beans.xml is located in WEB-INF, which can be found in the src/main/webapp directory of the example. Also in this directory we include index.html which uses a simple meta refresh to send the users browser to the Servlet, which is located at http://localhost:8080/jboss-as-helloworld/HelloWorld.

All the configuration files for this example are located in WEB-INF/, which can be found in the src/main/webapp directory of the example.

Notice that we don't even need a web.xml!

Let's start by taking a look at the servlet:


public class HelloWorldServlet extends HttpServlet {
   static String PAGE_HEADER = "<html><head /><body>";
(2)   static String PAGE_FOOTER = "</body></html>";
(3)   @Inject
   HelloService helloService;
   protected void doGet(HttpServletRequest req, HttpServletResponse resp)
         throws ServletException, IOException {
      PrintWriter writer = resp.getWriter();
      writer.println("<h1>" + helloService.createHelloMessage("World")
(4)            + "</h1>");


If you've used Servlet before, then you'll remember having to use xml to register your servlets. Fortunately, this is a thing of the past. Now all you need to do is add the @WebServlet annotation, and provide a mapping to a URL used to access the servlet. Much cleaner!


Every web page needs to be correctly formed HTML. We've created static Strings to hold the minimum header and footer to write out.


We inject the HelloService (a CDI bean) which generates the actual message. This allows to alter the implementation of HelloService at a later date without changing the view layer at all (assuming we don't alter the API of HelloService).


We call into the service to generate the message "Hello World", and write it out to the Http request.

Now we understand how the information is sent to the browser, let's take a look at the service.

public class HelloService {

   String createHelloMessage(String name) {
      return "Hello " + name + "!";

The service is very simple - no registration (XML or annotation) is required!