Getting Started Developing Applications

CDI + Servlet — Helloworld quickstart

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

Tip
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 Enterprise Application Platform 6 to be running (you can find out how to start the server in Installing and starting the JBoss server on Linux, Unix or Mac OS X or Installing and starting the JBoss server on Windows).

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

Tip

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 quickstart

It’s time to pull the covers back and dive into the internals of the quickstart.

Deploying the Helloworld quickstart using JBoss Developer Studio, or Eclipse with JBoss Tools

You may choose to deploy the quickstart using JBoss Developer Studio, or Eclipse with JBoss Tools. You’ll need to have JBoss Enterprise Application Platform 6 started in the IDE (as described in Starting the JBoss server from JBDS or Eclipse with JBoss Tools) and to have imported the quickstarts into Eclipse (as described in Importing the quickstarts into Eclipse).

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

Eclipse_Helloworld_Deploy_1

Make sure the correct server is selected, and hit Finish:

Eclipse_Deploy_2

You should see the server start up (unless you already started it in Starting the JBoss server from JBDS or Eclipse with JBoss Tools) and the application deploy in the Console log

Eclipse_Helloworld_Deploy_3

The helloworld quickstart in depth

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

The helloworld quickstart is comprised of a servlet and a CDI bean. We also include an empty beans.xml file, which tells JBoss Enterprise Application Platform 6 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. 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-helloworld/HelloWorld.

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

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

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

src/main/java/org/jboss/as/quickstarts/helloworld/HelloWorldServlet.java
@SuppressWarnings("serial")
@WebServlet("/HelloWorld")                                           (1)
public class HelloWorldServlet extends HttpServlet {

   static String PAGE_HEADER =
       "<html><head><title>helloworld</title></head><body>";                (2)

   static String PAGE_FOOTER = "</body></html>";

   @Inject
   HelloService helloService;                                        (3)

   @Override
   protected void doGet(HttpServletRequest req,
                        HttpServletResponse resp)
                        throws ServletException, IOException {
      resp.setContentType("text/html");
      PrintWriter writer = resp.getWriter();
      writer.println(PAGE_HEADER);
      writer.println("<h1>" +
                     helloService.createHelloMessage("World") +      (4)
                     "</h1>");
      writer.println(PAGE_FOOTER);
      writer.close();
   }

}
  1. 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!

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

  3. 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 ).

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

Tip

The package declaration and imports have been excluded from these listings. The complete listing is available in the quickstart source.

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

src/main/java/org/jboss/as/quickstarts/helloworld/HelloService.java
public class HelloService {

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

}

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

Share the Knowledge

Find this guide useful?

Feedback

Find a bug in the guide? Something missing? You can fix it by [forking the repository](http://github.com/jboss-jdf/jboss-as-quickstart), making the correction and [sending a pull request](http://help.github.com/send-pull-requests). If you're just plain stuck, feel free to ask a question in the [user discussion forum](http://jboss.org/jdf/forums/jdf-users).

Recent Changelog

  • Nov 04, 2013: Modified headers to remove setext titles Vineet Reynolds
  • Nov 01, 2013: Removed colons from document title Vineet Reynolds
  • Nov 01, 2013: Remove jboss as 7 references from quickstarts Sande Gilda
  • Jan 24, 2013: Compatibility updates for asciidoctor Dan Allen
  • Feb 28, 2013: Add missing </head> - thanks to franky! Pete Muir
  • Sep 17, 2012: Fixing jdf-78 Jason Porter
  • Jul 19, 2012: Fix issue #280: add setcontenttype to doget method in servlet and in guide Sande Gilda
  • Jul 19, 2012: Add author to the guide Pete Muir
  • Jul 10, 2012: Initial import of getting started developing applications guide Pete Muir

See full history »