Injecting the Data Access EJB

Open up the ExercisesServlet in the Eclipse Editor and take a look at the doGet() and doPost() stubs. Both the doGet() and doPost() request handlers will call our ExercisesDAO that we created previously, so we need to create an instance of it. We could do that ourselves, but it's a lot easier to let the EJB container manage it for us. All we need to do is include the @EJB annotation in front of (or directly above) the variable declaration:

Display a List of Exercises

The doGet() handler processes incoming GET requests, like the kind that the browser sends to the server to request a page. In our case, the function sets an attribute named "exercises" and populates it with the results of our exerciseDao's getAllExercises() method that we talked about above. The Servlet then brings up the exercises.jsp page with the updated Exercises list and blank entry form, ready to accept another exercise:

Allowing Entry of New Exercises

The exercises.jsp page uses the POST method to submit form data so that doPost() will handle form submissions. Only the name is required to instantiate a new Exercise, but we also want to store the mainMuscleWorked and otherMusclesWorked. That's done by calling the exerciseDao instance's persist() method, passing in a new Exercise() object. Finally, doGet() is called to refresh the exercise.jsp page with the new exercise added to the list:

Code the JSP Page

The last piece of our application is the exercises.jsp that was referenced in the Servlet's doGet() handler above. It provides the View part within the Model-View-Controller (MVC) architecture. It does that by including blocks of server-side code that generates content on the fly. You could say that it's dynamic content, but once it gets forwarded to the browser, it can't be changed again without using a front-end language like JavaScript.

To create the JSP file:

Right-click the WebContent node in the Package Explorer view and select New > JSP File from the popup menu. If you don't see that item in the list, select New > Other... instead to open the Wizard Selector dialog. The JSP File item is located under the Web folder.

On the New JSP File dialog, assign a name of "exercises.jsp" and click Next >.

Let's start by adding our imports. Just like in a java file, we must import classes that our not part of the java core. We will be needing java.util.List and our Exercise entity class. To import them, include the <%@page %> tag with the import attribute and set it to the two classes that we just mentioned. Separate each class with a comma.

Running the Application

At last the moment of truth has arrived. While it's true that a Java EE application involves a lot more overhead than your typical Hello World app, we did build an interactive multi-tier Web application! To try it out for yourself:

Right-click the WebContent node in the Package Explorer view and select Run As > Run on Server from the popup menu.

The first time you run your app, you'll have to select the server. Click the checkbox to always use the same server so that you don't have to select it every time.

Here is what the finished application looks like in the Internal Eclipse Browser:

Conclusion

This series on building a Java EE 6 application in Eclipse gave us a good overview of how version 3 EJBs, JPA, Servlets, JSP Page all work together to create a rich UI experience for the user. In future articles, we'll delve deeper into each of these technologies individually to learn more about what they bring to the table.

Rob Gravelle resides in Ottawa, Canada, and is the founder of GravelleWebDesign.com. Rob has built systems for Intelligence-related organizations such as Canada Border Services, CSIS as well as for numerous commercial businesses.

In his spare time, Rob has become an accomplished guitar player, and has released several CDs. His band, Ivory Knight, was rated as one Canada's top hard rock and metal groups by Brave Words magazine (issue #92) and reached the #1 spot in the National Heavy Metal charts on Reverb Nation.

Web Development Newsletter Signup

Advertiser Disclosure:
Some of the products that appear on this site are from companies from which QuinStreet receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. QuinStreet does not include all companies or all types of products available in the marketplace.

Thanks for your registration, follow us on our social networks to keep up-to-date