AdvancedProgrammingandTechnologiesWeek04Tutorialandworkshoptask f9f40f90 5cb2 49db Bda9 1749c7e7bff8 159290
AdvancedProgrammingandTechnologiesWeek04Tutorialandworkshoptask f9f40f90 5cb2 49db Bda9 1749c7e7bff8 159290
Project Description
In this project, you will develop a Todo Application using Java Servlets, JSP, and JDBC. This web
application will allow users to create, view, and delete todo items. The application follows the Model-
View-Controller (MVC) architecture pattern, which separates the application into three main components:
This project will help you understand core web development concepts using Java technologies and
reinforce your understanding of database operations, web application architecture, and user interface
design.
Learning Objectives
By completing this project, you will be able to:
Functionality Requirements
Your Todo Application must include the following features:
1. Add Todo: Users should be able to add new todo items with a title, description, and completion
status.
2. View Todos: Users should be able to view a list of all todo items.
3. Delete Todo: Users should be able to delete a todo item.
4. Navigation: Users should be able to navigate between different pages of the application.
Technical Requirements
Your implementation must adhere to the following technical requirements:
1. Java Servlet & JSP: Use Java Servlets for controllers and JSP for views.
2. JDBC: Use JDBC for database operations.
3. MySQL Database: Use a MySQL database to store todo items.
4. MVC Architecture: Follow the MVC architecture pattern.
5. Project Structure: Organize your code in a logical directory structure.
6. Error Handling: Implement proper error handling and display appropriate messages to users.
7. CSS Styling: Apply CSS styling to make the application visually appealing.
Project Setup
Download and install the latest version of Eclipse IDE for Enterprise Java and Web Developers.
Launch Eclipse and select a workspace location.
Download Apache Tomcat (version 10.0 or later) from the official website.
Extract the downloaded file to a location on your computer.
TodoApplication/
├── src/main/java/ # Java source files
│ ├── controller/ # Servlet classes
│ └── model/ # Model classes
├── src/main/webapp/ # Web resources
│ ├── WEB-INF/ # Configuration files
│ ├── assets/ # CSS, JS, and other static files
│ ├── view/ # JSP files
│ └── index.jsp # Home page
1. TodoModel.java : Represents a todo item with properties like id, title, description, and completed
status.
2. TodoDAO.java : Data Access Object that handles database operations for todo items.
TodoModel.java
package model;
// Constructor with id
public TodoModel(int id, String title, String description, boolean completed) {
this.id = id;
this.title = title;
this.description = description;
this.completed = completed;
}
@Override
public String toString() {
// Implement toString method
}
}
TodoDAO.java
package model;
import java.sql.*;
import java.util.ArrayList;
static {
// Load the MySQL JDBC driver
try {
Class.forName("com.mysql.cj.jdbc.Driver");
} catch (ClassNotFoundException e) {
throw new RuntimeException("Failed to load MySQL driver", e);
}
}
AddTodoServlet.java
package controller;
import model.TodoModel;
import model.TodoDAO;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
@WebServlet("/AddTodoServlet")
public class AddTodoServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
// 1. Retrieve form data (title, description, completed status)
// 4. Redirect to the list page after successful addition or show error message
}
}
ListTodoServlet.java
package controller;
import model.TodoModel;
import model.TodoDAO;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
@WebServlet("/ListTodoServlet")
public class ListTodoServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
// 1. Retrieve all todos from the database using TodoDAO
package controller;
import model.TodoModel;
import model.TodoDAO;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/DeleteTodoServlet")
public class DeleteTodoServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
// 1. Retrieve the todo ID from the request parameter
// 4. Redirect to the list page after successful deletion or show error message
}
}
Step 3: Creating the View Layer
The view layer displays the data to the user. For this project, you need to create three JSP files:
index.jsp
/* src/main/webapp/assets/css/styles.css */
@import url('https://fonts.googleapis.com/css2?family=Roboto:wght@400;700&display=swap');
body {
font-family: 'Roboto', sans-serif;
background-color: #f9f9f9;
max-width: 800px;
margin: 0 auto;
padding: 40px 20px;
}
h1 {
text-align: center;
}
2. Testing Functionality:
Model: Represents the data and business logic (TodoModel and TodoDAO).
View: Displays the data to the user (JSP files).
Controller: Handles user input and updates the model and view accordingly (Servlets).
2. JDBC Connection:
Always close database connections, statements, and result sets in a finally block or use try-with-
resources.
Use PreparedStatement to prevent SQL injection attacks.
3. Error Handling:
5. Debugging:
1. Update Functionality:
2. User Authentication:
4. Due Dates:
5. Categories or Tags:
6. Ajax Implementation:
Use Ajax to add, update, and delete todo items without page reloads.
7. Responsive Design:
Enhance the CSS to make the application fully responsive for various screen sizes.
8. Data Validation: