CDI + Servlet: Helloworld quickstart
====================================
:Author: Pete Muir

[[HelloworldQuickstart-]]

This quickstart shows you how to deploy a simple servlet to JBoss WildFly. 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 wildfly:deploy

The quickstart uses a Maven plugin to deploy the application. The plugin requires JBoss WildFly to be running (you can find out how to start the server in <<GettingStarted-on_linux, Installing and starting the JBoss server on Linux, Unix or Mac OS X>> or <<GettingStarted-on_windows, Installing and starting the JBoss server on Windows>>).

Now, check if the application has deployed properly by clicking http://localhost:8080/wildfly-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 wildfly:deploy` - deploy any changes to the application to the
  application server 

* `mvn wildfly: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 WildFly started in the IDE (as described  in <<GettingStarted-with_jboss_tools, Starting the JBoss server from JBDS or Eclipse with JBoss Tools>>) and to have imported the quickstarts into Eclipse (as described in <<GettingStarted-importing_quickstarts_into_eclipse,Importing the quickstarts into Eclipse>>).

With the quickstarts imported, you can deploy the quickstart by right clicking on the `wildfly-helloworld` project, and choosing _Run As -> Run On Server_:

image:gfx/Eclipse_Helloworld_Deploy_1.jpg[]

Make sure the correct server is selected, and hit Finish:
 
image:gfx/Eclipse_Deploy_2.jpg[]

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

image:gfx/Eclipse_Helloworld_Deploy_3.jpg[]


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 WildFly 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/wildfly-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
[source,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!

