1.4.4. Review the Quickstart Tutorials
1.4.4.1. Explore the helloworld Quickstart
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.
Procedure 1.9. Import the helloworld quickstart into JBoss Developer Studio
- If you have not done so, Section 2.3.2, “Configure the JBoss EAP 6 Maven Repository Using the Maven Settings”.
- If you have not done so, Section 1.3.1.3, “Install JBoss Developer Studio 5”.
- From the menu, select File → Import.
- In the selection list, choose Maven → Existing Maven Projects, then click Next.
Figure 1.9. Import Existing Maven Projects
- Browse to the
QUICKSTART_HOME/quickstart/helloworld/
directory and click OK. The Projects list box is populated with thepom.xml
file from the helloworld quickstart project.Figure 1.10. Select Maven Projects
- Click Finish.
Procedure 1.10. Build and Deploy the helloworld quickstart
- 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”.
- Right click on jboss-as-helloworld in the Project Explorer tab, and select Run As → Run on Server.
Figure 1.11. Run on Server
- Select the JBoss EAP 6.0 Runtime Server server and click Next. This deploys the helloworld quickstart to the JBoss server.
- 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
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.
- The
beans.xml
file is located in theWEB-INF/
folder in thesrc/main/webapp/
directory of the quickstart. - The
src/main/webapp/
directory also includes anindex.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. - All the configuration files for this example are located in
WEB-INF/
, which can be found in thesrc/main/webapp/
directory of the example. - Notice that the quickstart doesn't even need a
web.xml
file!
Procedure 1.12. Examine the Code
Review the HelloWorldServlet code
TheHelloWorldServlet.java
file is located in thesrc/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. Review the HelloService code
TheHelloService.java
file is located in thesrc/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.