Skip to content

Project 4

Putting It All Together

Concepts

The concepts we will be exploring with this project include:

  • Continuing use of git
  • HTML Form Processing
  • Session Management
  • Form Data Validation
  • EL
  • JSTL
  • JDBC
  • JavaBeans
  • The Model, View, Controller (MVC) Design Pattern

Special note on project code

Projects must rely on coded solutions that have been covered in the class material, unless otherwise specified.

If the coded solution uses concepts not covered in class, you are required to do a code review. This review must include a detailed explanation of your coding choices and rationale for employing a solution beyond the scope of the class material.

Project Overview

Note: This is the last push to demonstrate your proficiency with Git and GitHub. One way to do this is to make atomic commits with messages that fully describe the change and why it was made. This article might be helpful in improving your commit messages.


The new content in this project will explore the important subjects of form processing, session management, and database access. The application will allow users to add employees and search for employees on a web site. This project will also explore JDBC, JavaBeans, and the MVC design pattern. This combination represents a very modern and effective way to code web applications.

Specifications

Project Setup

  • Work done on project 4 can be done in parallel with finishing up project 3.
  • Be sure to complete Lab 1 - JDBC Startup.

Project Directory Structure

  • Your projects directory should now look like this.
projects/
|-- config
|-- dba
|-- docs
|-- lib
|-- output
|-- public_html
|   |-- images
|   |-- jsp
|-- src
    |-- java112
        |-- analyzer
        |-- employee
        |-- labs1
        |-- labs2
        |-- labs3
        |-- project2
        |-- project3
        |-- project4
        |-- utilities

This structure will hold all projects for the course. The new directories are in bold. Description of new directories:

  • The dba directory will hold the SQL script that is needed for the project.
  • The employee directory will contain classes pertaining to employees.
  • The project4 directory will contain the source code that is only part of project 4.
  • The public_html/jsp optional directory will hold some of your new JSP pages.

Your Web App

Application goals

  1. Allow a user to search for employees by, at minimum, first name, last name, or employee id.
  2. Allow a user to add a new employee.

Home Page

Your home page will need to be altered to add the links for this project:

  • A link to the project 4 Employee Search Page
  • A link to the project 4 Add Employee Page

Update your templating

There is a handy JSTL tag that allows us to replace the include directive that we used for templating in Unit 3. Research and implement the proper JSTL tag in your project, removing all unit 3 include directives.

Hint

The "include directive" from Unit 3 looked something like this:

<%@ include file="template-example-footer.jsp"%>

Project 4 Properties File

  • The project4.properties file have the usual information, like the author and the course, etc.
  • The file will also have entries to enable the application to establish a connection to a database. You will need the following entries:
    • driver – this will be the fully qualified name of the driver class
    • url – this will be the URL to the database
    • username – your mysql username
    • password – your mysql password

The Employee Package

Employee package

  • Create a directory named employee in your projects/src/java112 directory. This will hold the source for classes in the employee package.

Employee Class

  • Create a class named Employee and place it in the java112.employee package.
  • The class should minimally have the following String instance variables:
    • Employee id
    • First Name
    • Last Name
    • Social Security Number
    • Department
    • Room Number
    • Phone Number
  • Create get and set method for all the instance variables.
  • Create a toString method.

Search Class

  • Create a JavaBean class named Search and place it in the java112.employee package.
  • Add the following instance variables:
    • A String that holds the entered search type.
    • A String that holds the entered search term.
    • An ArrayList that holds the results from the database query
    • A boolean indicating whether or not the query found any employees.
  • Create get and set methods for the instance variables.
  • Don’t forget the empty constructor.
  • The class will also have a method named something like this with the same signature:

    public void addFoundEmployee(Employee employee) {
        ...
    }
    
  • This method will add the employee object to the List of found Employee objects.

EmployeeDirectory Class

Important!

  • Prepared statements are required in this class for all database queries.
  • Create a class EmployeeDirectory class in the java112.employee package.
  • Add an instance variable of type Properties. You will also need to import the package java.util if you don’t already.
  • Create a constructor for the class that has a Properties parameter.
  • Create a private method that will establish a connection to the database and return the connection to the calling method.
  • Create a method that will add a new record to the Employee table in the database. This method will have the following features:
    • The method will have an parameter for each of the instance variables in the Employee class.
    • The method will get a connection to the database.
    • The method will generate a SQL insert statement.
    • The method will run the correct JDBC code to insert the new employee into the database including all necessary exception handling.
  • Note: The following search methods have some some things in common. We will explore how to minimize duplication of code by using some common methods. This is a requirement for the project.
  • Create a public method for searching the employee database. It will have two parameters, the search term and the search type. It will return a Search object with the results. It will use the following methods to accomplish the search.
  • Create a private method that will search for an Employee in the database by employee id. It will have the following features:
    • The method will have a Search object as an parameter.
    • The method will get a connection to the database.
    • The method will generate a SQL select statement using the employee ID from the Search object in a where clause.
    • The method will run the query using the JDBC code including all necessary exception handling.
    • If the query returns any rows then set the boolean instance variable in the Search object to true. If the query does not return any rows then set the variable to false.
    • If the query returns rows, then the method will instantiate a new Employee object for each row and set its instance variables from the row from the database. Each new Employee object will be added to the Search object with the addFoundEmployee() method.
    • The method will close the connection.
  • Create a private method that will search for Employees by last name in the database. It will have the following features:
    • The method will have a Search object as an parameter.
    • The method will get a connection to the database.
    • The method will generate a SQL select statement using the last name from the Search object in a where clause.
    • The method will run the query using the JDBC code including all necessary exception handling.
    • If the query returns any rows then set the boolean instance variable in the Search object to true. If the query does not return any rows then set the variable to false.
    • If the query returns rows, then the method will instantiate a new Employee object for each row and set its instance variables from the row from the database. Each new Employee object will be added to the Search object with the addFoundEmployee() method.
    • The method will close the connection.
  • Create a private method that will search for Employees by first name in the database. It will have the following features:
    • The method will have a Search object as an parameter.
    • The method will get a connection to the database.
    • The method will generate a SQL select statement using the first name from the Search object in a where clause.
    • The method will run the query using the JDBC code including all necessary exception handling.
    • If the query returns any rows then set the boolean instance variable in the Search object to true. If the query does not return any rows then set the variable to false.
    • If the query returns rows, then the method will instantiate a new Employee object for each row and set its instance variables from the row from the database. Each new Employee object will be added to the Search object with the addFoundEmployee() method.
    • The method will close the connection.
  • Create other private methods as needed for the right factoring of the search to minimize, if not eliminate, duplicate code.

Application Startup Servlet

  • Create a servlet named ApplicationStartup that will performs some initialization for the application. Place it in the java112.project4 package.
  • This servlet will have add a new attribute to the @WebServlet annotation.
@WebServlet(
    name = "applicationStartup", 
    urlPatterns = { "/project4-startup" },
    loadOnStartup = 1
)
  • This annotation will cause the servlet to be loaded at startup and its init() method will be run.
  • The servlet will have an init method that will do the following:
    • Create a Properties object and load the project4.properties file.
    • Place the Properties object into the ServletContext with an attribute name that is different from the one used in other projects and labs. Something like “project4Properties” would work.
    • Create an EmployeeDirectory instance with the constructor that has a Properties parameter.
    • Place the EmployeeDirectory instance into the ServletContext.

Employee Search JSP Page

  • Create a JSP page that will allow the user to search for employees and place it in your public_html/jsp directory. Use the new template system for this page.
  • In the content portion of the page you should do the following.
    • Add an HTML form to the page that has the following features:
      • The action attribute will point to the Employee Search Servlet.
      • A text field for entering the search term.
      • A radio button set for selecting the search type. There will be 3 radio buttons, one for searching by ID, one for searching by Last Name, and one for searching by First Name.
    • A submit button.

Employee Servlet JSP Page Display Servlet

  • Create a servlet that has one simple purpose, to forward to the Employee Search JSP Page. We’re going to only access JSP pages through servlets in this project.
  • The servlet will only have a doGet() method which forwards to the JSP page.

Employee Search Results Servlet

  • Create a servlet that will perform searches for employees. Place it in the java112.project4 package.
  • This servlet will not need an init() method.
  • The doGet() method will perform the following:
    • Create a local variable that references the ServletContext.
    • Get the EmployeeDirectory instance from the ServletContext that was placed there by the Application Start Servlet.
    • Get the search type and the search term from the HTML form.
    • Search for the employees by calling the appropriate method in the EmployeeDirectory instance and pass the search type and the search term to the method.
    • Place the Search object into the session.
    • The servlet will forward the request and response to the Employee Search Results JSP page.

Employee Search Results JSP Page

  • Create a JSP page that will display the results of the search or a message that no employees were found.
  • The content of the page should use a table to display the results with each found employee in a row.
  • The HTML table and header row should be part of the template text of the page.
  • The table rows must be generated with JSTL.

Employee Add

Employee Add JSP Page

  • Create a JSP page that will allow the user to add a new employee and place it in your public_html/jsp directory.
  • This page will contain an HTML POST form with the following fields:
    • First Name
    • Last Name
    • Social Security Number
    • Department
    • Room Number
    • Phone Number
  • The form will submit to the Add New Employee servlet.
  • Remember to make the form’s method a POST!
  • Remember to validate the input!
  • The page will also have a EL display of a message. The value of the message will be set by the Add servlet and can display success or validation messages. Something like this will work.
<h3>${project4AddMessage}</h3>
  • Make sure that the add message only displays immediately after adding a new employee. If you go to the index page and return to the add page, the SUCCESS or validation message must be removed.

Employee Add JSP Page Display Servlet

  • Create a servlet that has one simple purpose, to forward to the Employee Add JSP Page. We’re going to only access JSP pages through servlets in this project.
  • The servlet will only have a doGet() method which forwards to the JSP page.

Employee Add Action Servlet

  • Create a servlet that will add a new Employee to the application. Place it in the java112.project4 package.
  • The servlet will access the EmployeeDirectory instance from the ServletContext that was placed there by the Application Start Servlet. This can be done with an init() method or in the doPost() method.
  • The doPost() method will perform the following:
    • Extract the data for the new Employee from the HTML form.
    • Call the add employee method in the EmployeeDirectory instance and pass the form data as arguments.
    • Add the returned message to the session.
    • Send a redirect the browser to the Add Employee page. (NOT a forward this time.)
  • NOTE: It’s a doPost() method, not a doGet().

Rubric

All of the following must be satisfied to achieve a "Met" status

Criteria Met Status
Debugging & Problem-Solving Code is free from errors.
Code Quality Code is exceptionally clean, efficient, and maintainable. Follows best practices, coding standards, and programming principles.
Directory Structure Organizes the project directory structure as specified, creating new directories for the project components.
Home Page The home page to include links for accessing the Employee Search and Add Employee functionality.
Templating Replaces include directives with proper JSTL tags for template inclusion.
Properties The project4.properties file contains correct information, including database connection details.
Employee Package
  • Creates an Employee class with appropriate instance variables, getters, setters, and a toString method.
  • Develops a Search JavaBean class with the required instance variables, getters, setters, and methods for adding found employees.
  • Constructs an EmployeeDirectory class with proper constructors, methods for adding employees to the database, and methods for searching employees in the database based on different criteria.
Application Startup Creates an ApplicationStartup servlet that initializes the application by loading properties and setting up the EmployeeDirectory instance in the ServletContext.
Employee Search
  • Designs a JSP page for employee search that includes an HTML form for user input, allowing users to select search type and enter search terms.
  • Implements a servlet to forward to the Employee Search JSP Page.
  • Develops a servlet for performing employee searches, retrieves data from the EmployeeDirectory, sets search results in a Search object, and forwards the request to the Employee Search Results JSP Page.
  • Creates a JSP page to display search results, including a table with employee data generated using JSTL.
Employee Add
  • Designs a JSP page for adding new employees with an HTML form, validates user input, and displays success or validation messages.
  • Implements a servlet to forward to the Employee Add JSP Page.
  • Develops a servlet for adding new employees, extracts form data, calls the appropriate method in EmployeeDirectory, handles messages, and performs a redirect to the Add Employee page.
Code Documentation All classes, methods, instance variables, and constructors are thoroughly documented with accurate descriptions and proper JavaDoc comments.
External Sources External sources (websites, classmates, AI tools, etc), if utilized, are referenced and documented within the code as comments.
Reflection Issue created correctly with thoughtful answers to the reflection questions.