EJB 3.1 Application with Stateless Session Bean on Glassfish 3.0 and NetBeans

June 18th, 2010 by Usha Informatique Team Leave a reply »

Enterprise Java Beans (EJB) is a bit complex topic before the release of EJB 3.0 specification. In the EJB 3.0 specification the things are much simplified now from many perspectives

- Development of application using EJB

- Maintaining application having EJB

- Understanding of EJB for a new beginner

We have developed a sample application (with both local and remote interface) which illustrates creation of EJB and using it in a servlet. The infrastructure parts of the application are as follows

- NetBeans 6.8

- Glassfish Application Server (v 3.0)

The steps that needs to be followed are as follows

- Create a New Java EE Project SampleEJBApplication in Netbeans. Just follow the instruction provided by Netbeans.

- There would be three folders created ( SampleEJBApplication, SampleEJBApplication-ejb, SampleEJBApplication-war )

EJB Application

- In the SampleEJBApplication-ejb, create a package called com.sample.service

- Create a Local Interface called Calculator.java as follows

package com.sample.service;

import javax.ejb.Local;

/**
*
* @author Shashank
*/
@Local
public interface Calculator
{

public int calculate (int start, int end);

}

- Look out for @Local annotation which declare the interface to be local

- Create a class called CalculatorBean.java as follows which implements the local interface

package com.sample.service;

import javax.ejb.EJB;
import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;

/**
*
* @author Shashank
*/
@Stateless

@EJB(name=”java:global/MyCalculatorBean”, beanInterface=Calculator.class)
public class CalculatorBean implements Calculator {

public String getServerInfo () {
return “This is glass fish server using EE6 version”;
}

public int calculate(int start, int end) {
return start+end;

}

public int remoteCalculate(int start, int end) {
return start+end;
}

}

Notice few things in the above class

- @Stateless annotation which declares the bean to be stateless

- @EJB(name=”java:global/MyCalculatorBean”, beanInterface=Calculator.class), it says that when referring to this bean using JNDI it could be called using  java:global/MyCalculatorBean

Front End Application (SampleEJBApplication-war)

- Inside the Source folder, create a package called servlets

- Now there are two approach using which you can refer to local EJB

Approach 1

- Use @EJB annotation inside the servlet to call the local EJB ( Available with Glassfish and not JBoss :) )

Approach 2

- Use JNDI lookup inside the servlet to call the local EJB

- Create a servlet called TestServlet.java using Netbeans

- For Approach 1 the code will go as follows

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType(”text/html;charset=UTF-8″);
PrintWriter out = response.getWriter();

Calculator calculatorBean;
try {

Context context = new InitialContext();
calculatorBean = (Calculator) context.lookup(”java:global/MyCalculatorBean”);
out.println(”<html>”);
out.println(”<head>”);
out.println(”<title>Servlet TestServlet</title>”);
out.println(”</head>”);
out.println(”<body>”);
out.println(”<h1>Servlet TestServlet at ” + request.getContextPath() + “</h1>”);
out.println(calculatorBean.calculate(100, 200));
out.println(”</body>”);
out.println(”</html>”);

} catch (NamingException nex) {
nex.printStackTrace();
} finally {
out.close();
}
}

- Please notice as how the bean is referred using the JNDI name defined in bean class

- The same bean can be retrieved by using

a) (Calculator) context.lookup(”com.sample.service.Calculator”) (Non Portable JNDI lookup)

b) java:global/SampleEJBApplication/SampleEJBApplication-ejb/CalculatorBean!com.sample.service.Calculator (Portable JNDI lookup)

- For Approach 2, the code would go as follows

@EJB
private Calculator calculatorBean;
/**
* 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 {

out.println(”<html>”);
out.println(”<head>”);
out.println(”<title>Servlet TestServlet</title>”);
out.println(”</head>”);
out.println(”<body>”);
out.println(”<h1>Servlet TestServlet at ” + request.getContextPath () + “</h1>”);
out.println(calculatorBean.calculate(100, 200));
out.println(”</body>”);
out.println(”</html>”);

} finally {
out.close();
}
}

- Notice the usage of @EJB annotation with no issues regarding JNDI

- Run the code in both cases and should return 300 as the result

The implemtation for the remote interface would be given in next post.

Enjoy EJB 3.0 !!

Ushainformatique Development Team

Advertisement

14 comments

  1. Thanks for a great post and interesting comments. I found this post while searching for some popular lyrics. Thanks for sharing this post.

  2. Great information! I’ve been looking for something like this for a while now. Thanks!

  3. What a great resource!

  4. Amiable post and this post helped me alot in my college assignement. Say thank you you as your information.

  5. Zachi says:

    Can I see code where the bean have both local & remote.
    And the two different clients (local/remote)

  6. That is really interesting. It provided me a few ideas and I’ll be writing them on my blog shortly. I’m bookmarking your blog and I’ll be back again. Thanks again!

  7. You areawesome! This website is really great. I definitely wish much more people study this and get what youre stating, mainly because let me tell you, its important things. I never wouldve imagined regarding it by doing this unless Id run into your weblog. Thanks for putting it up. I wish you’ve excellent success.

  8. Very high honor and privilege to landlord’s blog, the article I was very moved.

  9. Thanks for this article! I still see these TV shows to date, and I never get tired of them, great shows.

  10. Terrific work! This is the type of information that should be shared around the web. Shame on the search engines for not positioning this post higher!

  11. Someone I work with visits your blog frequently and recommended it to me to read too. The writing style is great and the content is top-notch. Thanks for the insight you provide the readers!

  12. Nice dispatch and this enter helped me alot in my college assignement. Thank you for your information.

  13. Incredibly useful. I love the way you write. Do you provide an RSS feed?

  14. hcg drops says:

    A refreshing point of view written in a compelling and interesting way. Nice.