Creating Stateless Session Bean

A Session Bean encapsulates business logic in methods, which in turn are executed by a client. This way, the business logic is separated from the client.

Stateless Session Beans do not maintain state. This means that when a client invokes a method in a Stateless bean, the bean is ready to be reused by another client. The information stored in the bean is generally discarded when the client stops accessing the bean.

This type of bean is mainly used for persistence purposes, since persistence does not require a conversation with the client.

It is not in the scope of this recipe to learn how Stateless Beans work in detail. If you wish to learn more, please visit:

http://jcp.org/en/jsr/detail?id=318

or

https://www.packtpub.com/developer-guide-for-ejb3/book

In this recipe, we will see how to use NetBeans to create a Stateless Session Bean that retrieves information from the database, passes through a servlet and prints this information on a page that is created on-the-fly by our servlet.

Getting ready

It is required to have NetBeans with Java EE support installed to continue with this recipe.

If this particular NetBeans version is not available in your machine, please visit http://download.netbeans.org.

We will use the GlassFish Server in this recipe since it is the only Server that supports Java EE 6 at the moment.

We also need to have Java DB configured. GlassFish already includes a copy of Java DB in its installation folder. If you wish to learn how to configure Java DB refer to the Chapter 4, JDBC and NetBeans.

It is possible to follow the steps on this recipe without the previous code, but for better understanding we will continue to build on the top of the previous recipes source code.

How to do it...

  1. Right-click on EJBApplication node and select New and Session Bean....
  2. For Name and Location: Name the EJB as ManufacturerEJB.
  3. Under Package, enter beans.
  4. Leave Session Type as Stateless.
  5. Leave Create Interface with nothing marked and click Finish.
How to do it...

Here are the steps for us to create business methods:

  1. Open ManufacturerEJB and inside the class body, enter:
    @PersistenceUnit
    EntityManagerFactory emf;
    public List findAll(){
    return emf.createEntityManager().createNamedQuery("Manufacturer.findAll").getResultList();
    }
    
  2. Press Ctrl+Shift+I to resolve the following imports:
    java.util.List;
    javax.persistence.EntityManagerFactory;
    javax.persistence.PersistenceUnit;
    

Creating the Servlet:

  1. Right-click on the EJBApplication node and select New and Servlet....
  2. For Name and Location: Name the servlet as ManufacturerServlet.
  3. Under package, enter servlets.
  4. Leave all the other fields with their default values and click Next.
  5. For Configure Servlet Deployment: Leave all the default values and click Finish.

With the ManufacturerServlet open:

After the class declaration and before the processRequest method, add:

@EJB
ManufacturerEJB manufacturerEJB;

Then inside the processRequest method, first line after the try statement, add:

List<Manufacturer> l = manufacturerEJB.findAll();

Remove the /* TODO output your page here and also */.

And finally replace:

out.println("<h1>Servlet ManufacturerServlet at " + request.getContextPath () + "</h1>");

With:

for(int i = 0; i < 10; i++ )
out.println("<b>City</b> " + l.get(i).getCity() + ", <b>State</b> " + l.get(i).getState() +"<br>" );

Resolve all the import errors and save the file.

How it works...

To execute the code produced in this recipe, right-click on the EJBApplication node and select Run.

When the browser launches append to the end of the URL/ManufacturerServlet, hit Enter.

Our application will return City and State names.

One of the coolest features in Java EE 6 is that usage of web.xml can be avoided if annotating the servlet. The following code does exactly that:

@WebServlet(name="ManufacturerServlet", urlPatterns={"/ManufacturerServlet"})

Since we are working on Java EE 6, our Stateless bean does not need the daunting work of creating interfaces, the @Stateless annotation takes care of that, making it easier to develop EJBs.

We then add the persistence unit, represented by the EntityManagerFactory and inserted by the @PersistenceUnit annotation.

Finally we have our business method that is used from the servlet. The findAll method uses one of the named queries from our entity to fetch information from the database.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.146.152.71