Posts Tagged ‘EJB 3.0 application with a Servlet’


Warning: strtotime() [function.strtotime]: It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'America/Chicago' for 'CDT/-5.0/DST' instead in /home/ushainfo/public_html/blog/wp-includes/functions.php on line 35

Warning: strtotime() [function.strtotime]: It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'America/Chicago' for 'CDT/-5.0/DST' instead in /home/ushainfo/public_html/blog/wp-includes/functions.php on line 35

Warning: date() [function.date]: It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'America/Chicago' for 'CDT/-5.0/DST' instead in /home/ushainfo/public_html/blog/wp-includes/functions.php on line 43

Warning: date() [function.date]: It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'America/Chicago' for 'CDT/-5.0/DST' instead in /home/ushainfo/public_html/blog/wp-includes/functions.php on line 43

Warning: strtotime() [function.strtotime]: It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'America/Chicago' for 'CDT/-5.0/DST' instead in /home/ushainfo/public_html/blog/wp-includes/functions.php on line 35

Warning: strtotime() [function.strtotime]: It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'America/Chicago' for 'CDT/-5.0/DST' instead in /home/ushainfo/public_html/blog/wp-includes/functions.php on line 35

Warning: date() [function.date]: It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'America/Chicago' for 'CDT/-5.0/DST' instead in /home/ushainfo/public_html/blog/wp-includes/functions.php on line 43

Warning: date() [function.date]: It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'America/Chicago' for 'CDT/-5.0/DST' instead in /home/ushainfo/public_html/blog/wp-includes/functions.php on line 43

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


Warning: strtotime() [function.strtotime]: It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'America/Chicago' for 'CDT/-5.0/DST' instead in /home/ushainfo/public_html/blog/wp-includes/functions.php on line 35

Warning: strtotime() [function.strtotime]: It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'America/Chicago' for 'CDT/-5.0/DST' instead in /home/ushainfo/public_html/blog/wp-includes/functions.php on line 35

Warning: date() [function.date]: It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'America/Chicago' for 'CDT/-5.0/DST' instead in /home/ushainfo/public_html/blog/wp-includes/functions.php on line 107

Warning: date() [function.date]: It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'America/Chicago' for 'CDT/-5.0/DST' instead in /home/ushainfo/public_html/blog/wp-includes/functions.php on line 107

Warning: date() [function.date]: It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'America/Chicago' for 'CDT/-5.0/DST' instead in /home/ushainfo/public_html/blog/wp-includes/functions.php on line 109

Warning: date() [function.date]: It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'America/Chicago' for 'CDT/-5.0/DST' instead in /home/ushainfo/public_html/blog/wp-includes/functions.php on line 109

Warning: date() [function.date]: It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'America/Chicago' for 'CDT/-5.0/DST' instead in /home/ushainfo/public_html/blog/wp-includes/functions.php on line 111

Warning: date() [function.date]: It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'America/Chicago' for 'CDT/-5.0/DST' instead in /home/ushainfo/public_html/blog/wp-includes/functions.php on line 111

Warning: date() [function.date]: It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'America/Chicago' for 'CDT/-5.0/DST' instead in /home/ushainfo/public_html/blog/wp-includes/functions.php on line 112

Warning: date() [function.date]: It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'America/Chicago' for 'CDT/-5.0/DST' instead in /home/ushainfo/public_html/blog/wp-includes/functions.php on line 112
June 18th, 2010

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