0% found this document useful (0 votes)
19 views12 pages

Java Web Application - Workshop 2 - Appointment Management Schedule WS25

Java Web Application - workshop 2 - appointment management schedule WS25

Uploaded by

huyenvhnse184472
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views12 pages

Java Web Application - Workshop 2 - Appointment Management Schedule WS25

Java Web Application - workshop 2 - appointment management schedule WS25

Uploaded by

huyenvhnse184472
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Java Web Application: Manage Appointment Schedules (MVC2 : Main Controller)

Objective

Develop a Java web application to manage appointment schedules, allowing users to book, view, and
manage appointments. The application should support user authentication, appointment creation,
modification, cancellation, and reminders.

Requirements

No. Function Description Evaluation Criteria Marks


Implement a registration form (register.jsp) to Form validation (unique
1 User Registration collect user details (username, password, username, password 1
email). strength)
Create a login form (login.jsp) to authenticate Secure authentication
2 User Login 1
users against stored credentials. (hashing, HTTPS)
Session management
Implement logout functionality to invalidate
3 User Logout (invalidate session on 1
user sessions.
logout)
Provide a dashboard (dashboard.jsp) for users
Data visualization
4 Dashboard to view upcoming appointments and quick 1
(calendar view, summary)
actions.
Allow users to book new appointments
Create Form to input and save
5 (createAppointment.jsp) by selecting date, 1
Appointment appointment details
time, and purpose.
View Display a list (viewAppointments.jsp) of all Tabular/list view of
6 1
Appointments appointments with options to edit or cancel. appointments
Form to update
Edit Allow users to edit details of an existing
7 appointment details and 1
Appointment appointment (editAppointment.jsp).
save changes
Update appointment
Cancel Implement functionality to cancel an
8 status to 'Cancelled' in the 1
Appointment appointment from the list of appointments.
database
Send reminders (sendReminders.jsp) to users
Appointment Notification mechanism
9 for their upcoming appointments via email or 1
Reminders (email, SMS)
SMS.
Display data in a table with
Appointment Allow users to view their past appointments
10 details of past 1
History (appointmentHistory.jsp).
appointments
Note: You write the code again, using the main controller MVC2

Step 1: Create Database Schema

Create the necessary tables in your database. For instance:

CREATE TABLE Users (


id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(50) NOT NULL UNIQUE,
password VARCHAR(255) NOT NULL,
email VARCHAR(100) NOT NULL
);

CREATE TABLE Appointments (


id INT AUTO_INCREMENT PRIMARY KEY,
userId INT,
appointmentDate DATE,
appointmentTime TIME,
purpose VARCHAR(255),
status VARCHAR(50),
FOREIGN KEY (userId) REFERENCES Users(id)
);

Step 2: Implement the Model

1. Database Utility Class

package com.example.util;
import java.sql.Connection;
import java.sql.DriverManager;

public class DatabaseConnection {


public static Connection initializeDatabase() throws Exception {
String dbDriver = "com.mysql.cj.jdbc.Driver";
String dbURL = "jdbc:mysql://localhost:3306/";
String dbName = "appointment_db";
String dbUsername = "root";
String dbPassword = "password";

Class.forName(dbDriver);
Connection conn = DriverManager.getConnection(dbURL + dbName, dbUsername, dbPassword);
return conn;
}
}
2. User Model

package com.example.model;

public class User {


private int id;
private String username;
private String password;
private String email;

3. Appointment Model

package com.example.model;

import java.util.Date;

public class Appointment {


private int id;
private int userId;
private Date appointmentDate;
private Date appointmentTime;
private String purpose;
private String status;
}

Step 3: Implement the Controller

1. User Controller (UserServlet.java)

package com.example.controller;

import com.example.model.User;
import com.example.util.DatabaseConnection;
import org.mindrot.jbcrypt.BCrypt;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

@WebServlet("/UserServlet")
public class UserServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
String action = request.getParameter("action");

switch (action) {
case "register":
registerUser(request, response);
break;
case "login":
loginUser(request, response);
break;
case "logout":
logoutUser(request, response);
break;
}
}

private void registerUser(HttpServletRequest request, HttpServletResponse response) throws


IOException {
String username = request.getParameter("username");
String password = request.getParameter("password");
String email = request.getParameter("email");

String hashedPassword = BCrypt.hashpw(password, BCrypt.gensalt());

try {
Connection conn = DatabaseConnection.initializeDatabase();
PreparedStatement stmt = conn.prepareStatement("INSERT INTO Users (username, password,
email) VALUES (?, ?, ?)");
stmt.setString(1, username);
stmt.setString(2, hashedPassword);
stmt.setString(3, email);
stmt.executeUpdate();
stmt.close();
conn.close();
response.sendRedirect("login.jsp");
} catch (Exception e) {
e.printStackTrace();
}
}

private void loginUser(HttpServletRequest request, HttpServletResponse response) throws


IOException {
String username = request.getParameter("username");
String password = request.getParameter("password");

try {
Connection conn = DatabaseConnection.initializeDatabase();
PreparedStatement stmt = conn.prepareStatement("SELECT * FROM Users WHERE username
= ?");
stmt.setString(1, username);
ResultSet rs = stmt.executeQuery();

if (rs.next()) {
String storedPassword = rs.getString("password");
if (BCrypt.checkpw(password, storedPassword)) {
HttpSession session = request.getSession();
session.setAttribute("userId", rs.getInt("id"));
session.setAttribute("username", username);
response.sendRedirect("dashboard.jsp");
} else {
response.sendRedirect("login.jsp?error=invalid");
}
} else {
response.sendRedirect("login.jsp?error=invalid");
}
stmt.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}

private void logoutUser(HttpServletRequest request, HttpServletResponse response) throws


IOException {
HttpSession session = request.getSession();
session.invalidate();
response.sendRedirect("login.jsp");
}
}

2. Appointment Controller (AppointmentServlet.java)

package com.example.controller;

import com.example.model.Appointment;
import com.example.util.DatabaseConnection;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
@WebServlet("/AppointmentServlet")
public class AppointmentServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
String action = request.getParameter("action");

switch (action) {
case "create":
createAppointment(request, response);
break;
case "edit":
editAppointment(request, response);
break;
case "cancel":
cancelAppointment(request, response);
break;
}
}

private void createAppointment(HttpServletRequest request, HttpServletResponse response) throws


IOException {
HttpSession session = request.getSession();
int userId = (int) session.getAttribute("userId");
String appointmentDate = request.getParameter("appointmentDate");
String appointmentTime = request.getParameter("appointmentTime");
String purpose = request.getParameter("purpose");

try {
Connection conn = DatabaseConnection.initializeDatabase();
PreparedStatement stmt = conn.prepareStatement("INSERT INTO Appointments (userId,
appointmentDate, appointmentTime, purpose, status) VALUES (?, ?, ?, ?, ?)");
stmt.setInt(1, userId);
stmt.setString(2, appointmentDate);
stmt.setString(3, appointmentTime);
stmt.setString(4, purpose);
stmt.setString(5, "Scheduled");
stmt.executeUpdate();
stmt.close();
conn.close();
response.sendRedirect("dashboard.jsp");
} catch (Exception e) {
e.printStackTrace();
}
}

private void editAppointment(HttpServletRequest request, HttpServletResponse response) throws


IOException {
int id = Integer.parseInt(request.getParameter("id"));
String appointmentDate = request.getParameter("appointmentDate");
String appointmentTime = request.getParameter("appointmentTime");
String purpose = request.getParameter("purpose");

try {
Connection conn = DatabaseConnection.initializeDatabase();
PreparedStatement stmt = conn.prepareStatement("UPDATE Appointments SET
appointmentDate = ?, appointmentTime = ?, purpose = ? WHERE id = ?");
stmt.setString(1, appointmentDate);
stmt.setString(2, appointmentTime);
stmt.setString(3, purpose);
stmt.setInt(4, id);
stmt.executeUpdate();
stmt.close();
conn.close();
response.sendRedirect("viewAppointments.jsp");
} catch (Exception e) {
e.printStackTrace();
}
}

private void cancelAppointment(HttpServletRequest request, HttpServletResponse response) throws


IOException {
int id = Integer.parseInt(request.getParameter("id"));

try {
Connection conn = DatabaseConnection.initializeDatabase();
PreparedStatement stmt = conn.prepareStatement("UPDATE Appointments SET status =
'Cancelled' WHERE id = ?");
stmt.setInt(1, id);
stmt.executeUpdate();
stmt.close();
conn.close();
response.sendRedirect("viewAppointments.jsp");
} catch (Exception e) {
e.printStackTrace();
}
}

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws


ServletException, IOException {
String action = request.getParameter("action");

if ("reminder".equals(action)) {
sendReminders(request, response);
}
}
private void sendReminders(HttpServletRequest request, HttpServletResponse response) {
// Implement the reminder functionality (e.g., using JavaMail API for emails)
}
}

Step 4: Implement the Views

1. register.jsp

<form action="UserServlet?action=register" method="post">


Username: <input type="text" name="username" required><br>
Password: <input type="password" name="password" required><br>
Email: <input type="email" name="email" required><br>
<input type="submit" value="Register">
</form>

2. login.jsp

<form action="UserServlet?action=login" method="post">


Username: <input type="text" name="username" required><br>
Password: <input type="password" name="password" required><br>
<input type="submit" value="Login">
</form>
<%
String error = request.getParameter("error");
if ("invalid".equals(error)) {
%>
<p style="color:red">Invalid username or password</p>
<%
}
%>

3. dashboard.jsp

<%@ page import="java.sql.*" %>


<%@ page import="javax.servlet.http.HttpSession" %>
<%
HttpSession session = request.getSession(false);
if (session == null || session.getAttribute("username") == null) {
response.sendRedirect("login.jsp");
}

int userId = (int) session.getAttribute("userId");


Connection conn = DatabaseConnection.initializeDatabase();
PreparedStatement stmt = conn.prepareStatement("SELECT * FROM Appointments WHERE userId = ?
AND status = 'Scheduled'");
stmt.setInt(1, userId);
ResultSet rs = stmt.executeQuery();
%>
<h2>Dashboard</h2>
<p>Welcome, <%= session.getAttribute("username") %></p>
<a href="UserServlet?action=logout">Logout</a>

<h3>Upcoming Appointments</h3>
<table border="1">
<tr>
<th>Date</th>
<th>Time</th>
<th>Purpose</th>
<th>Status</th>
</tr>
<%
while (rs.next()) {
%>
<tr>
<td><%= rs.getDate("appointmentDate") %></td>
<td><%= rs.getTime("appointmentTime") %></td>
<td><%= rs.getString("purpose") %></td>
<td><%= rs.getString("status") %></td>
</tr>
<%
}
stmt.close();
conn.close();
%>
</table>

<a href="createAppointment.jsp">Create New Appointment</a>


<a href="viewAppointments.jsp">View All Appointments</a>
<a href="appointmentHistory.jsp">View Appointment History</a>

4. createAppointment.jsp

<%@ page import="javax.servlet.http.HttpSession" %>


<%
HttpSession session = request.getSession(false);
if (session == null || session.getAttribute("username") == null) {
response.sendRedirect("login.jsp");
}
%>
<form action="AppointmentServlet?action=create" method="post">
Date: <input type="date" name="appointmentDate" required><br>
Time: <input type="time" name="appointmentTime" required><br>
Purpose: <input type="text" name="purpose" required><br>
<input type="submit" value="Create Appointment">
</form>
<a href="dashboard.jsp">Back to Dashboard</a>

5. viewAppointments.jsp

<%@ page import="java.sql.*" %>


<%@ page import="javax.servlet.http.HttpSession" %>
<%
HttpSession session = request.getSession(false);
if (session == null || session.getAttribute("username") == null) {
response.sendRedirect("login.jsp");
}

int userId = (int) session.getAttribute("userId");


Connection conn = DatabaseConnection.initializeDatabase();
PreparedStatement stmt = conn.prepareStatement("SELECT * FROM Appointments WHERE userId
= ?");
stmt.setInt(1, userId);
ResultSet rs = stmt.executeQuery();
%>
<h2>All Appointments</h2>
<table border="1">
<tr>
<th>Date</th>
<th>Time</th>
<th>Purpose</th>
<th>Status</th>
<th>Actions</th>
</tr>
<%
while (rs.next()) {
%>
<tr>
<td><%= rs.getDate("appointmentDate") %></td>
<td><%= rs.getTime("appointmentTime") %></td>
<td><%= rs.getString("purpose") %></td>
<td><%= rs.getString("status") %></td>
<td>
<a href="editAppointment.jsp?id=<%= rs.getInt("id") %>">Edit</a> |
<a href="AppointmentServlet?action=cancel&id=<%= rs.getInt("id") %>">Cancel</a>
</td>
</tr>
<%
}
stmt.close();
conn.close();
%>
</table>
<a href="dashboard.jsp">Back to Dashboard</a>
6. editAppointment.jsp

<%@ page import="java.sql.*" %>


<%@ page import="javax.servlet.http.HttpSession" %>
<%
HttpSession session = request.getSession(false);
if (session == null || session.getAttribute("username") == null) {
response.sendRedirect("login.jsp");
}

int id = Integer.parseInt(request.getParameter("id"));
Connection conn = DatabaseConnection.initializeDatabase();
PreparedStatement stmt = conn.prepareStatement("SELECT * FROM Appointments WHERE id = ?");
stmt.setInt(1, id);
ResultSet rs = stmt.executeQuery();
if (rs.next()) {
%>
<form action="AppointmentServlet?action=edit" method="post">
<input type="hidden" name="id" value="<%= id %>">
Date: <input type="date" name="appointmentDate" value="<%= rs.getDate("appointmentDate") %>"
required><br>
Time: <input type="time" name="appointmentTime" value="<%= rs.getTime("appointmentTime") %>"
required><br>
Purpose: <input type="text" name="purpose" value="<%= rs.getString("purpose") %>" required><br>
<input type="submit" value="Save Changes">
</form>
<%
}
stmt.close();
conn.close();
%>
<a href="viewAppointments.jsp">Back to Appointments</a>

7. appointmentHistory.jsp

<%@ page import="java.sql.*" %>


<%@ page import="javax.servlet.http.HttpSession" %>
<%
HttpSession session = request.getSession(false);
if (session == null || session.getAttribute("username") == null) {
response.sendRedirect("login.jsp");
}

int userId = (int) session.getAttribute("userId");


Connection conn = DatabaseConnection.initializeDatabase();
PreparedStatement stmt = conn.prepareStatement("SELECT * FROM Appointments WHERE userId = ?
AND status = 'Completed'");
stmt.setInt(1, userId);
ResultSet rs = stmt.executeQuery();
%>
<h2>Appointment History</h2>
<table border="1">
<tr>
<th>Date</th>
<th>Time</th>
<th>Purpose</th>
<th>Status</th>
</tr>
<%
while (rs.next()) {
%>
<tr>
<td><%= rs.getDate("appointmentDate") %></td>
<td><%= rs.getTime("appointmentTime") %></td>
<td><%= rs.getString("purpose") %></td>
<td><%= rs.getString("status") %></td>
</tr>
<%
}
stmt.close();
conn.close();
%>
</table>
<a href="dashboard.jsp">Back to Dashboard</a>

You might also like