Java Web Application - Workshop 2 - Appointment Management Schedule WS25
Java Web Application - Workshop 2 - Appointment Management Schedule WS25
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
package com.example.util;
import java.sql.Connection;
import java.sql.DriverManager;
Class.forName(dbDriver);
Connection conn = DriverManager.getConnection(dbURL + dbName, dbUsername, dbPassword);
return conn;
}
}
2. User Model
package com.example.model;
3. Appointment Model
package com.example.model;
import java.util.Date;
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;
}
}
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();
}
}
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();
}
}
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;
}
}
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();
}
}
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();
}
}
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();
}
}
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)
}
}
1. register.jsp
2. login.jsp
3. dashboard.jsp
<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>
4. createAppointment.jsp
5. viewAppointments.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