Hi, this is Chad “Shod” Darby. In this video,
I’ll give you an introduction to Enterprise Java Beans. Java EE 7 includes support for EJB 3.2, so
Enterprise Java Beans provide a standardized way to implement solutions for server side
business logic. EJBs are basically POJOs but they’re managed by an EJB container that runs
inside of your Java EE app server. You can use EJBs also as JMS listeners so it makes
your enterprise application more scalable without having to write additional code. There’s actually four types of Enterprise
Java Beans. You have something called the stateless session bean, which contains business
logic but no client state. There’s also a stateful session bean, contains business logic
and client state. There’s also a singleton session bean, which is instantiated only once
and shared throughout the application, and finally, there’s a message-driven bean, enhanced
for listening for messages on a JMS queue or topic. All right, so let’s take a look at how EJBs
work by walking through a small demo. What we’re going to do is we’re going to create
an EJB called a Hello World EJB. We’re simply going to provide a method to say hello. Then
we’ll also have a servlet who’s going to call methods on the EJB, so a Hello World servlet
will make a link through the EJB and call the method Say Hello. Let’s go ahead and move into our Eclipse project.
Look at the source code for lesson 31. I’ll expand the folders here. I’ll move down to
the EJB package and I’ll take a look at this file here, Hello World Bean.java. This is
the source code that we saw in the previous example, so stateless local bean, Hello World
bean, and then on lines 20 through 22, we have the implementation of our method, say
hello. This is the method that another application
will call, our servlet will actually call this method and will simply return the words
hello world. That’s it for the coding for our Enterprise Java Bean. Now let’s go ahead
and take a look at our servlet. We move into the package here, client, I’ll access the
file Hello World servlet. This is just a very basic servlet. What we’ll do is we’ll make use of resource
injections to get a reference to the EJB inside of our servlet. What we’ll do here is we’ll
just make use of this EJB annotation and we get hello world bean and we give our variable
name my bean, so the GlassFish app server will inject the bean at this reference here
or set the variable accordingly. Then I can move down to the do get meta for
the servlet, make use of the print writer and here I say, out.println(myBean.sayhello()).
myBean is our variable reference to the Enterprise Java Bean that was injected by the Java EE
app server, and we simply say mybean.sayhello, so we call that method on the EJB and it will
return the results in this servlet page. Let’s go ahead and run this application, so
I’ll just do a right click on our Hello World servlet, and I’ll say run as, run on server.
I’ll just go ahead and choose our GlassFish 4 server, check the box to always use this
server, hit finish, and now it’s going to actually deploy the application to the grassroots
server. Once it’s deployed, we’ll see the output and so here we have hello world, so
that’s actual call that our server is making through the EJB. The EJB returns that Hello
World string, and then we display it in the servlet’s output page. Good job, so now what I want to do is actually
make a small modification to our EJB. I’d like to be able to pass in a parameter to
it. I’ll pass in a name and then our EJB will use that name in the string that it returns.
Here I have a parameter coming in as string name, and then I’ll simply append it to these
Hello World statements. I’ll say plus name and I’ll just clean it up and save it. Here,
we simply return hello world and whatever name they pass in to call this EJB.
Now what I want to do is I’ll move back over to our servlet and I’ll just make a modification
here on this say hello. Now I’ll actually pass in a name and I’ll pass in the name of
Johnny. Now that I made the modification, let’s go ahead and run our application again,
so I’ll simply right click, I’ll say run as, run on server and it will deploy the new version
to the app server and then we’ll see the output hello world, Johnny. This all looks great.
This is exactly what we’re looking for. We are successful in having our servlet make
calls to our Enterprise Java Bean, good job. Well that wraps up our video. In this vide,
I gave you an introduction to Enterprise Java Beans. We created an EJB and then we called
it from a Java servlet.