Create a Java Enterprise Application Controller in Eclipse

By Rob Gravelle

Create a Java Enterprise Application Controller in Eclipse

Enterprise JavaBeans were created as server-side components that were supposed to facilitate rapid and simplified development of distributed, transactional, secure and portable applications. Unfortunately, their designers were not wildly successful on either the rapid or simplified development fronts. Today, with the EJB 3.1 spec in production, things have changed - and for the better might I add!

Today we'll be adding a Servlet Class to our Exercises Catalog application that will accept requests from the browser and invoke a DAO method accordingly. It will function as the application controller within the MVC model. We've already developed the Model in the Persistence in Java EE Applications article, and Defining JPA Persistence Units in your Java Enterprise Applications.


Before we proceed any further, I'd like to point out a couple of minor errors from the last tutorial. It was only when I finally ran my finished project that I noticed the issues.

The persistence.xml File Location

In the instructions, I wrote to create the persistence.xml file in a META-INF directory under the Java Resources/src folder. That is not entirely correct for an Eclipse environment because the project already includes a META-INF directory under the WebContent folder. That is where you should place your persistence.xml file.

The getAllExercises() Query

The JPA specification utilizes JPQL, which stands for Java Persistence Query Language. Although it is based on SQL, there are some differences. One of them, is that it does not recognize "SELECT *" for SELECT ALL. Instead, you should use the table alias as in "SELECT e FROM Exercise e". Here is the revised code for getAllExercises():

// Retrieves all the exercises:
public List<Exercise> getAllExercises() {
    TypedQuery<Exercise> query = em.createQuery(
        "SELECT e FROM Exercise e", Exercise.class);
    return query.getResultList();

About MVC Architecture

Model-View-Controller (MVC) is a highly popular software architectural pattern for modern multi-tier applications. It divides an application into three parts, so as to minimize dependencies while maximizing scalability. It was incorporated into Smalltalk-76 by Trygve Reenskaug in the 1970s. MVC defines three tiers as follows:

  1. The Model represents the data and may contain some behavior pertaining to it.
  2. The View handles the representation of the information to the user. Multiple views of the same information are possible, depending on the data or how it's utilized. For instance, the same data might be presented as a bar chart for management and as a tabular view for the marketing team.
  3. The Controller accepts input and converts it to commands for the model or view according to the business rules of the application. Hence, it contains the brains of the operation (logic).

Here is an illustration from the site that shows how all the part work together:


Create the Servlet File

The part that we are focusing on here today, the Controller, will act as a traffic cop that directs requests from the browser to the correct process. Usually, these processes reside in separate class files, but today's coding will be simple enough that we can put a lot of it in the least for now.

To create the Servlet:

  1. Begin by opening the Exercises Catalog application that we started working on back in the Create a Java EE Web Application using the Glassfish Server article.
  2. Right-click your project's src folder in the Project Explorer and select New > Servlet from he popup list. If you don't see the Servlet item, select Other... instead and select it from the full list. You'll find it under the Web folder.
  3. On the Create Servlet dialog, name the class "ExercisesServlet" and leave the Java package field blank. That will create the class in the Default package.
  4. Click the Next button to proceed to the servlet deployment descriptor information screen.
  5. Click the Edit button beside the URL mappings and change it to "/". The .do extension is a Servlet convention that implies "do something".
  6. Again, click the Next button.
  7. Under the "Which method stubs would you like to create?" question, deselect the "Constructor from superclass" checkbox and make sure that both the doPost and doGet methods are checked.
  8. Click the Finish button to close the dialog and create the Servlet class.

Here is what should appear in the Editor:

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

 * Servlet implementation class ExercisesServlet
public class ExercisesServlet extends HttpServlet {
        private static final long serialVersionUID = 1L;

         * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
                // TODO Auto-generated method stub

         * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
                // TODO Auto-generated method stub



In today's article we learned about the role of the controller in MVC architecture. Next month, we'll inject the ExercisesDAO class as a managed EJB and code the doGet() and doPost() methods to handle requests from a JSP page.

Rob Gravelle

Rob Gravelle resides in Ottawa, Canada, and is the founder of 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.

Make a Comment

Loading Comments...

  • Web Development Newsletter Signup

    Invalid email
    You have successfuly registered to our newsletter.
Thanks for your registration, follow us on our social networks to keep up-to-date