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
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!