import com.example.bongotasks.SampleTasks;
import com.example.bongotasks.Task;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@WebServlet(name = "TaskServlet", value = "/TaskServlet")
public class TaskServlet extends HttpServlet {
// SampleTasks sampleTasks = new SampleTasks();
public static Task taskToUpdate = new Task("0", "0", "0", false);
// connection to the database
protected Connection getConnection() {
Connection connection = null;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
// Variables that are used for database connections
String sqlURL = "jdbc:mysql://127.0.0.1:3306/?user=root";
String sqlUsername = "root";
String sqlPassword = "admin";
connection = DriverManager.getConnection(sqlURL, sqlUsername, sqlPassword);
} catch (SQLException | ClassNotFoundException e) {
System.out.println("Error: " + e.getMessage());
}
return connection;
}
protected List<Task> getTasks(HttpServletRequest request, HttpServletResponse response) throws SQLException, ServletException, IOException {
List<Task> taskList = new ArrayList<>();
try (Connection connection = getConnection();
PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM bongotasks.tasks")) {
ResultSet resultSet = preparedStatement.executeQuery();
System.out.println(resultSet);
while (resultSet.next()) {
String id = resultSet.getString("id");
String name = resultSet.getString("name");
String description = resultSet.getString("description");
boolean status = resultSet.getBoolean("status");
taskList.add(new Task(id, name, description, status));
// System.out.println("Task: " + id + "|" + name + "|" + description + "|" + status);
}
} catch (SQLException e) {
System.out.println(e.getMessage());
}
request.setAttribute("taskList", taskList);
request.getRequestDispatcher("/taskDashboard.jsp").forward(request, response);
return taskList;
}
protected Map<String, String> addTask(HttpServletRequest request, HttpServletResponse response) throws SQLException, ServletException, IOException {
String taskName = request.getParameter("taskName");
String taskDesc = request.getParameter("taskDesc");
try (Connection connection = getConnection();
PreparedStatement preparedStatement = connection.prepareStatement("INSERT INTO bongotasks.tasks (name, description, status) VALUES (?, ?, ?)")) {
preparedStatement.setString(1, taskName);
preparedStatement.setString(2, taskDesc);
preparedStatement.setBoolean(3, false);
preparedStatement.executeUpdate();
} catch (SQLException e) {
System.out.println(e.getMessage());
}
Map<String, String> newTask = new HashMap<>();
newTask.put("taskName", taskName);
newTask.put("taskDesc", taskDesc);
request.setAttribute("newTask", newTask);
request.getRequestDispatcher("/confirmCreate.jsp").forward(request, response);
return newTask;
}
protected Task fillTask(HttpServletRequest request, HttpServletResponse response) throws SQLException, ServletException, IOException {
String editId = request.getParameter("edit-id");
request.setAttribute("id", editId);
try (Connection connection = getConnection();
PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM bongotasks.tasks WHERE id = " + editId)) {
ResultSet resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
String id = resultSet.getString("id");
String name = resultSet.getString("name");
String description = resultSet.getString("description");
boolean status = resultSet.getBoolean("status");
taskToUpdate = new Task(id, name, description, status);
}
request.setAttribute("task", taskToUpdate);
request.getRequestDispatcher("/editTaskForm.jsp").forward(request, response);
return taskToUpdate;
} catch (SQLException e) {
System.out.println(e.getMessage());
}
return null;
}
protected Task updateTask(HttpServletRequest request, HttpServletResponse response) throws SQLException, ServletException, IOException {
String taskName = request.getParameter("taskName");
String taskDesc = request.getParameter("taskDesc");
try (Connection connection = getConnection();
PreparedStatement preparedStatement = connection.prepareStatement("UPDATE bongotasks.tasks SET name = '" + taskName + "', description = '" + taskDesc + "' WHERE id = " + taskToUpdate.getId())) {
preparedStatement.executeUpdate();
} catch (SQLException e) {
System.out.println(e.getMessage());
}
request.setAttribute("taskName", taskName);
request.setAttribute("taskDesc", taskDesc);
request.setAttribute("newtaskName", taskToUpdate.getName());
request.setAttribute("newtaskDesc", taskToUpdate.getDescription());
request.getRequestDispatcher("/confirmUpdate.jsp").forward(request, response);
return new Task(taskToUpdate.getId() ,taskToUpdate.getName(), taskToUpdate.getDescription(), taskToUpdate.isStatus());
}
protected void updateStatus(HttpServletRequest request, HttpServletResponse response) throws SQLException, ServletException, IOException {
String taskId = request.getParameter("id");
String taskStatus = request.getParameter("status");
try (Connection connection = getConnection();
PreparedStatement preparedStatement = connection.prepareStatement("UPDATE bongotasks.tasks SET status = " + taskStatus + " WHERE id = " + taskId)) {
preparedStatement.executeUpdate();
} catch (SQLException e) {
System.out.println(e.getMessage());
}
response.sendRedirect(request.getContextPath() + "/");
}
protected void deleteTask(HttpServletRequest request, HttpServletResponse response) throws SQLException, ServletException, IOException {
String deleteId = request.getParameter("delete-id");
request.setAttribute("id", deleteId);
try (Connection connection = getConnection();
PreparedStatement preparedStatement = connection.prepareStatement("DELETE FROM bongotasks.tasks WHERE id = " + deleteId)) {
preparedStatement.executeUpdate();
} catch (SQLException e) {
System.out.println(e.getMessage());
}
request.getRequestDispatcher("/confirmDelete.jsp").forward(request, response);
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String servletPath = request.getServletPath();
try {
switch (servletPath) {
case "/TaskServlet/addTask":
addTask(request, response);
break;
case "/TaskServlet/updateTask":
updateTask(request, response);
break;
case "/TaskServlet/deleteTask":
deleteTask(request, response);
break;
case "/TaskServlet/dashboard":
getTasks(re
A to-do list made using JavaEE and JSP.zip
需积分: 0 117 浏览量
更新于2023-12-29
收藏 4.22MB ZIP 举报
JavaEE和JSP是开发Web应用程序的常用技术栈。在这个"A to-do list"项目中,开发者利用了JavaEE的企业级特性以及JSP(JavaServer Pages)的动态网页生成能力,创建了一个任务管理应用。让我们深入了解一下这两个技术以及如何在实践中结合它们。
**JavaEE概述**
JavaEE(Java Platform, Enterprise Edition),也称为Java EE或 Jakarta EE,是一个开源的、跨平台的应用程序框架,用于构建企业级的分布式、多层的Web应用。它提供了许多服务和组件,如Servlet、JSP、EJB(Enterprise JavaBeans)、JMS(Java Message Service)等,使得开发人员可以轻松地处理网络应用的复杂性。
**JSP技术**
JSP是JavaEE的一部分,主要用于创建动态网页。JSP文件包含HTML、XML或者其他标记语言,同时嵌入了Java代码片段。这些Java代码可以在服务器端运行,生成响应的HTML内容并发送到客户端。JSP的主要优点是将表现层(视图)与业务逻辑分离,有利于提高代码的可维护性和重用性。
**Servlet技术**
Servlet是JavaEE中的核心组件之一,它是服务器端的Java应用程序,用于接收和响应HTTP请求。在本项目中,Servlet可能被用来处理用户提交的任务添加、删除和修改请求,然后更新后台数据库。Servlets通常与JSP一起使用,JSP负责显示视图,而Servlet负责处理业务逻辑。
**数据库集成**
一个任务清单应用通常需要存储和检索任务数据。JavaEE提供JDBC(Java Database Connectivity)API来连接和操作数据库。开发者可能使用了诸如MySQL、Oracle或者PostgreSQL这样的关系型数据库,并通过编写Java代码来执行SQL查询,实现数据的增删改查。
**MVC模式**
在JavaEE和JSP的项目中,Model-View-Controller(MVC)设计模式很常见。在这个应用中,"Model"代表业务逻辑和数据模型,"View"是用户看到的页面,"Controller"是Servlet,负责处理请求并将结果传递给View。通过这种架构,代码组织更加清晰,易于维护。
**Web容器**
JavaEE应用通常部署在Web容器上,如Tomcat、Jetty或WildFly。这些容器负责加载和运行JavaEE应用,提供必要的服务,如HTTP服务、会话管理、安全管理等。
**项目结构**
在"A to-do list made using JavaEE and JSP"项目中,我们可以期待看到以下目录结构:
- `WEB-INF`:包含`web.xml`部署描述符文件,`classes`目录存放编译后的Java类,`lib`目录存放依赖的JAR库。
- `jsp`目录:包含所有的JSP页面,如`index.jsp`、`taskList.jsp`等。
- `resources`目录:可能包含数据库连接配置文件、国际化资源文件等。
**总结**
这个"A to-do list"项目展示了如何使用JavaEE和JSP开发一个简单的Web应用。通过Servlet处理请求,JSP生成动态内容,以及可能的数据库交互,实现了任务的管理功能。了解这些技术及其相互作用对于理解Web开发的基础和进阶概念至关重要。
zero2100
- 粉丝: 172
- 资源: 2460
最新资源
- 基于c语言的线性链表的实现和应用
- 艾利和iriver Astell&Kern SP3000 V1.20升级固件
- 律师事务所网站建设与管理功能概述
- Python 端口访问邮件提醒工具
- 基于springboot的抗疫物资管理系统
- 基于C语言的二叉树构建及遍历
- 587833617736230KEY_C0091 STM32简易交通灯仿真设计.zip
- 垃圾废物检测19-YOLO(v5至v11)、COCO、CreateML、Paligemma、TFRecord、VOC数据集合集.rar
- 专项资金申报平台需求规范文档解析及关键技术要求
- TMS320F28377原理图
- Docker以及Docker-Compose的安装与卸载
- 艾利和iriver Astell&Kern SP3000 V1.31升级固件
- 基于C语言的图的实现和遍历
- 周勤富恒升职业学校网络安全渗透测试及解决方案第2版.doc
- images(5).zip
- 计算机程序设计员三级(选择题)