JavaEE WildFly Quickstart(1) - CDI + Servlet: Helloworld quickstart

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值