What you will learn: 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.
Deploying the Helloworld quickstart using JBoss Developer Sutdio
Follow the tutorial from Redhat
In depth perspective of helloworld project
src/main/webapp/
- /WEB-INF/beans.xml – tells JBoss WildFly to look for beans in this application and to activate the CDI
- /index.html – uses a meta refresh to send the users browser to the Servlet, which is located at http://localhost:8080/wildfly-helloworld/HelloWorld
- /WEB-INF – is where all the configuration files are located
Notice: we don’t even need a web.xml? (why?)
-
src/main/java/org/jboss/as/quickstarts/helloworld/HelloWorldServlet.java
@SuppressWarnings("serial")
@WebServlet("/HelloWorld")
public class HelloWorldServlet extends HttpServlet {
static String PAGE_HEADER = "<html><head><title>helloworld</title></head><body>";
static String PAGE_FOOTER = "</body></html>";
@Inject
HelloService helloService;
@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") + "</h1>");
writer.println(PAGE_FOOTER);
writer.close();
}
}
- (1) We used to use xml to register our servlets, however now all we need to do is add the @WebServlet annotation and provide a mapping to a URL used to access the servlet. (Where is the URL provided?)
- (2) Every web page needs to be correctly formed HTML, static strings are used to hold the minimum header and footer to write out
- (3) HelloService (a CDI bean) is injected to generate the actual message. This allows to alter implementation of HelloService later without changing the view layer assuming the API of HelloService is not changed.
- (4) Call into the service to generate the message “Hello World” and write it out to the HTTP request.
- (5) Also note the package declaration and imports