Adding a webclient to EJB project

This tutorial goes on where The previous tutorial stopped so please make sure to check it out before reading anay further!

Ok let's get going again, open the ejb project in your intellij IDE and follow these simple steps:

  • Click file -> new module 
  • Create new module from scratch -> next
  • name the module webclient -> next
  • create source directory src -> next
  • select application server and web application (should be checked when you check application server). Again make sure to select your jboss 6.1.0 final server.
  • Click finish
Now that we have our webclient module we also want to add it to our EAR artifact the same way as we added the EJB artifact to the EAR file (Enterprise Archive). The ear file is in fact one big file that contains all our components we want to use, in our case a backend EJB and a front end webclient module.

In Intellij click file -> project structure. In this dialog once again go to Artifacts, click on the ejb:ear exploded artifact and webclient:war exploded to it (which is under avaible elements -> artifacts). Our superior IDE once again suggest to fix all of our problems, just let him :). Click fix -> apply -> ok.

Run your server and you should see a simple page: "Place your content here".
Next up we will use a servlet to test our EJB hello world bean. Change the index.jsp file to the folowing:
<jsp:forward page="/myservlet"/>


Next add the following class to the src map of the webclient module: (oh yeah.. you can just delete the web.xml since we dont need that with the @WebServlet annotation).
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(urlPatterns = "/myservlet")
public class WelcomeServlet extends HttpServlet {

    @EJB
    private HelloUser userBean;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       String name = userBean.sayHello("EjbMasters");
       req.setAttribute("name",name);
       req.getRequestDispatcher("hello.jsp").forward(req,resp);
    }
}

Note: @EJB makes the container autowire our EJB bean so we do not have to use JNDI lookup in the code! Go EJB3!

Note: HelloUser is not recognized yet but just put your cursor in it and press alt+enter ->add dependency on 'ejb' module.

Now add the hello.jsp page in the same folder as the index.jsp page:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>

  Simple jsp page
  Welcome, ${requestScope.name}


Ok that's pretty much it, go to run configurations of your jboss server. Set startup page to: http://localhost:8080/webclientWeb and then go to the deployment tab. Notice that the ejb:war exploded has replaced our ejb:ear exploded.. remove it and add the ejb:ear again!

Now if you run your application you will see: Hello, EjbMasters!
Congratualations you got yourself a working EJB webapplication, cheers!