1.4.4. Review the Quickstart Tutorials

1.4.4.1. Explore the helloworld Quickstart

Summary

The helloworld quickstart shows you how to deploy a simple Servlet to JBoss EAP 6. The business logic is encapsulated in a service which is provided as a CDI (Contexts and Dependency Injection) bean and injected into the Servlet. This quickstart is very simple. All it does is print "Hello World" onto a web page. It is a good starting point to make sure you have configured and started your server properly.

Detailed instructions to build and deploy this quickstart using a command line can be found in the README file at the root of the helloworld quickstart directory. Here we show you how to use JBoss Developer Studio to run the quickstart.

Procedure 1.9. Import the helloworld quickstart into JBoss Developer Studio

If you previously imported all of the quickstarts into JBoss Developer Studio following the steps here Section 1.4.3.1, “Run the Quickstarts in JBoss Developer Studio”, you can skip to the next section.
  1. From the menu, select FileImport.
  2. In the selection list, choose MavenExisting Maven Projects, then click Next.
    Import Existing Maven Projects

    Figure 1.9. Import Existing Maven Projects

  3. Browse to the QUICKSTART_HOME/quickstart/helloworld/ directory and click OK. The Projects list box is populated with the pom.xml file from the helloworld quickstart project.
    Select Maven Projects

    Figure 1.10. Select Maven Projects

  4. Click Finish.

Procedure 1.10. Build and Deploy the helloworld quickstart

  1. If you have not yet configured JBoss Developer Studio for JBoss EAP 6, you must Section 1.3.1.5, “Add the JBoss EAP 6 Server to JBoss Developer Studio”.
  2. Right click on jboss-as-helloworld in the Project Explorer tab, and select Run AsRun on Server.
    Run on Server

    Figure 1.11. Run on Server

  3. Select the JBoss EAP 6.0 Runtime Server server and click Next. This deploys the helloworld quickstart to the JBoss server.
  4. To verify that the helloworld quickstart was deployed successfully to the JBoss server, open a web browser and access the application at this URL: http://localhost:8080/jboss-as-helloworld

Procedure 1.11. Examine the Directory Structure

The code for the helloworld quickstart can be found in the QUICKSTART_HOME/helloworld directory. The helloworld quickstart is comprised a Servlet and a CDI bean. It also includes an empty beans.xml file which tells JBoss EAP 6 to look for beans in this application and to activate the CDI.
  1. The beans.xml file is located in the WEB-INF/ folder in the src/main/webapp/ directory of the quickstart.
  2. The src/main/webapp/ directory also includes an index.html file which uses a simple meta refresh to redirect the user's browser to the Servlet, which is located at http://localhost:8080/jboss-as-helloworld/HelloWorld.
  3. 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.
  4. Notice that the quickstart doesn't even need a web.xml file!

Procedure 1.12. Examine the Code

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

    The HelloWorldServlet.java file is located in the src/main/java/org/jboss/as/quickstarts/helloworld/ directory. This Servlet sends the information to the browser.
    27. @WebServlet("/HelloWorld")
    28. public class HelloWorldServlet extends HttpServlet {
    29. 
    30.    static String PAGE_HEADER = "<html><head /><body>";
    31.
    32.    static String PAGE_FOOTER = "</body></html>";
    33.
    34.    @Inject
    35.    HelloService helloService;
    36.
    37.    @Override
    38.    protected void doGet(HttpServletRequest req, HttpServletResponse resp) 
                                 throws ServletException, IOException {
    39.       PrintWriter writer = resp.getWriter();
    40.       writer.println(PAGE_HEADER);
    41.       writer.println("<h1>" + helloService.createHelloMessage("World") + "</h1>");
    42.       writer.println(PAGE_FOOTER);
    43.       writer.close();
    44.     }
    45. 
    46. }
    
    

    Table 1.1. HelloWorldServlet Details

    Line Note
    27 Before Java EE 6, an XML file was used to register Servlets. It is now much cleaner. All you need to do is add the @WebServlet annotation and provide a mapping to a URL used to access the servlet.
    30-32 Every web page needs correctly formed HTML. This quickstart uses static Strings to write the minimum header and footer output.
    34-35 These lines inject the HelloService CDI bean which generates the actual message. As long as we don't alter the API of HelloService, this approach allows us to alter the implementation of HelloService at a later date without changing the view layer.
    41 This line calls into the service to generate the message "Hello World", and write it out to the HTTP request.
  2. Review the HelloService code

    The HelloService.java file is located in the src/main/java/org/jboss/as/quickstarts/helloworld/ directory. This service is very simple. It returns a message. No XML or annotation registration is required.
     9. public class HelloService {
    10. 
    11.    String createHelloMessage(String name) {
    12.       return "Hello " + name + "!"; 
    32.    }
    33. }
    34.