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.
We will see three projects in left side project navigation windows as below
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 .
Here you see HelloEJB module under Enterprise Beans tab
3. Create a servlet under web application to call EJB.
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.
What is "demo.session.string.service.StringServiceBeanRemote; ". I can't see in your tutorial. Please mail me, thank you.
ReplyDeleteGood explanation, thank you.
ReplyDeleteThanks Sukumar
DeleteThis comment has been removed by the author.
ReplyDeleteI am happy for sharing on this blog its awesome blog I really impressed. thanks for sharing. Great efforts.
ReplyDeleteLearn 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 .
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.
ReplyDeleteJava training in chennai | Java training in annanagar | Java training in omr | Java training in porur | Java training in tambaram | Java training in velachery
This comment has been removed by the author.
ReplyDeleteI think this is one of the best blog for me because this is really helpful for me. Thanks for sharing this valuable information
ReplyDeleteinternship meaning | internship meaning in tamil | internship work from home | internship certificate format | internship for students | internship letter | Internship completion certificate | internship program | internship certificate online | internship graphic design
Mbidacompme_1981 German Cirulis https://wakelet.com/wake/ls9MtMDghA47bgwez8UjC
ReplyDeletenabseiteti
itadXtranku Jordan Shamoon Click
ReplyDeleteAwesome
nalematme
Good content. You write beautiful things.
ReplyDeletesportsbet
mrbahis
hacklink
sportsbet
mrbahis
hacklink
vbet
taksi
vbet
Good text Write good content success. Thank you
ReplyDeletetipobet
betpark
bonus veren siteler
poker siteleri
kralbet
slot siteleri
kibris bahis siteleri
betmatik
salt likit
ReplyDeletesalt likit
dr mood likit
big boss likit
dl likit
dark likit
ZBBA
ReplyDeleteشركة تنظيف فرش
تنظيف فرش