Archive for the ‘EJB’ Category
Wrox #5: Enterprise Java Beans (EJB) Tutorial Overview
Last Updated on Thursday, 9 April 2015 Written by Chad Darby Monday, 13 April 2015
In this video, I will give you an overview of Enterpise Java Beans (EJBs).
Please SUBSCRIBE to my YouTube channel.
Follow luv2code with the links below:
You may have noticed that things were quiet around here. Well, a couple of months ago I was hired by Wrox to create video tutorials for the book, 24 Hour Java. I created a total of 73 videos (whew) for the book. So the project took up all of my free time that I normally used for YouTube videos. Anyways, the project is over and now I have some free time on the weekends 🙂
The book’s author is Yakov Fain and he’s an Oracle Java champion. It was a real pleasure working with Yakov on this project.
Wrox gave me permission to publish a small set of the videos on my YouTube channel for promotional purposes. Over the next couple of weeks, I’ll release a new video from the book on my YouTube channel. You can check the playlist here: http://goo.gl/cemsm2
If you’re interested in the book, then head over to book’s website using the link below.
Java Programming 24-Hour Trainer, 2nd Edition
Here’s the official blurb about the book.
Quick and painless Java programming with expert multimedia instruction
Java Programming 24-Hour Trainer, 2nd Edition is your complete beginner’s guide to the Java programming language, with easy-to-follow lessons and supplemental exercises that help you get up and running quickly. Step-by-step instruction walks you through the basics of object-oriented programming, syntax, interfaces, and more, before building upon your skills to develop games, web apps, networks, and automations.
This second edition has been updated to align with Java SE 8 and Java EE 7, and includes new information on GUI basics, lambda expressions, streaming API, WebSockets, and Gradle. Even if you have no programming experience at all, the more than six hours of Java programming screencasts will demonstrate major concepts and procedures in a way that facilitates learning and promotes a better understanding of the development process.
This is your quick and painless guide to mastering Java, whether you’re starting from scratch or just looking to expand your skill set. Master the building blocks that go into any Java project Make writing code easier with the Eclipse tools Learn to connect Java applications to databases Design and build graphical user interfaces and web applications Learn to develop GUIs with JavaFX
If you want to start programming quickly, Java Programming 24-Hour Trainer, 2nd Edition is your ideal solution.
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.