Hello World EJB 3.1 Application using Netbean

This post explains developing simple Hello world application in Net Bean . It explains ease of EJB 3.1 development using  annotations in you java code. Coding, deploying and using an enterprise bean never been so easy. Here we are going to develop a Stateless Session bean and going to invoke it from a Servlet.

A Remote Interface for bean

package demo.session;
import javax.ejb.Remote;
/**
* @author Yogesh
*/
@Remote
public interface HelloEJBRemote {
   String getMessage();
}

 

Here @remote annotation is being used for demoting Remote Interface for EJB Module. It declares one public method which is by default business method.

Bean Implantation Class -

package demo.session;

import javax.ejb.Stateless;
/**
* @author Yogesh
*/
@Stateless
public class HelloEJB implements HelloEJBRemote {

public String getMessage() {
return "Hello ";
    }
}

This is a simple implementation class which implement HelloEJBRemote interface method. Only one annotation @stateless is being used here to denote that it is a Stateless session bean.

Here we are done with code for developing an enterprise java bean. For ease of testing i have used NetBean 6.8 to deploy and test out bean. Here are the steps to create an EJB in NetBean

1. Create an Enterprise application with EJB module as well as Web application module.

 Fullscreen capture 1292010 54825 PM.bmp

Fullscreen capture 1292010 54855 PM.bmp

We will see three projects in left side project navigation windows as below

Fullscreen capture 1292010 54953 PM.bmp

Here HelloEnterpriseApp is an enterprise aaplication project which includes both HelloEnterpriseApp-ejb and HelloEnterpriseApp-war projects.

2. Create Bean remote Interface and bean implementation java under HelloEnterpriseApp-ejb projects and insert above code respectively .

Fullscreen capture 1292010 55050 PM.bmp

Here you see HelloEJB module under Enterprise Beans tab

3. Create a servlet under web application to call EJB.

Fullscreen capture 1292010 55138 PM.bmp

Servlet code is

package demo.servlet;

import demo.session.HelloEJB;

import demo.session.HelloEJBRemote;
import demo.session.string.service.StringServiceBeanRemote;
import java.io.IOException;
import java.io.PrintWriter;
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;

/**
*
* @author ydevatra
*/
@WebServlet(name="EBJClientServlet", urlPatterns={"/EBJClientServlet"})
public class EBJClientServlet extends HttpServlet {

    @EJB
    private HelloEJBRemote ejbObj;
    @EJB
    private StringServiceBeanRemote strService;

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            // TODO output your page here
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet EBJClientServlet</title>"); 
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet EBJClientServlet at " + request.getContextPath () + "</h1>");
            out.println("<h1>EJB Message " + ejbObj.getMessage() + "</h1>");
            out.println("<h1>String Service EJB Message " + strService.processString(ejbObj.getMessage()) + "</h1>");
            out.println("</body>");
            out.println("</html>");
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}

Here Servlet code call ejb on doGet request.

Here we have declared HelloEJBRemote typed class instance variable as ejbObj and note here that @EJB annotation is added to this declaration. It means to container that instantiate HelloEJB’s instance and assign to ejbObj. Container will take care of lookup, narrowing and creating remote object. So we are ready to use instantiated ejb instance in processRequest() method  by calling remote method “getMessage”.

This example explains how easy development is with use of annotations in JEE 6.

Comments

  1. What is "demo.session.string.service.StringServiceBeanRemote; ". I can't see in your tutorial. Please mail me, thank you.

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. I am happy for sharing on this blog its awesome blog I really impressed. thanks for sharing. Great efforts.

    Learn SAP from the Industry Experts we bridge the gap between the need of the industry. eTechno Soft Solutions provide the Best IT Training in Bangalore .

    ReplyDelete
  4. Great post! I am actually getting ready to across this information, It’s very helpful for this blog.Also great with all of the valuable information you have Keep up the good work you are doing well.
    Java training in chennai | Java training in annanagar | Java training in omr | Java training in porur | Java training in tambaram | Java training in velachery


    ReplyDelete
  5. This comment has been removed by the author.

    ReplyDelete

Post a Comment

Popular posts from this blog

Composite Design Pattern by example

State Design Pattern by Example

Eclipse command framework core expression: Property tester