I would like to write an article for those who are writing applications for the first time using Java. We would appreciate it if you could use it as a reference for creating training assignments for your portfolio and company. This time, we will create a task manager. By applying this, you can also use it to create Twitter clones.
I will post articles for each function of the application.
eclipse4.16.0 Tomcat9 Java11 Mysql5.7
table of contents
Ask for confirmation of task details here before deleting a task
task-delete.java
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Delete task</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">
</head>
<body>
<jsp:useBean id="task" scope="request" class="model.entity.TaskBean"/>
<%
String category_name = (String)request.getAttribute("category_name");
String status_name = (String)request.getAttribute("status_name");
String user_name = (String)request.getAttribute("user_name");
int version = (int)request.getAttribute("version");
%>
<jsp:include page="header.jsp"/>
<div class="contain mt-4 mr-5 ml-5">
<div class="row justify-content-center">
<h3>Delete task</h3>
</div>
<table class="table">
<thead>
<tr>
<th scope="col"></th>
<th scope="col">Do you want to delete this task?</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">Task ID</th>
<td><jsp:getProperty property="task_id" name="task"/></td>
</tr>
<tr>
<th scope="row">Task name</th>
<td><jsp:getProperty property="task_name" name="task"/></td>
</tr>
<tr>
<th scope="row">Category</th>
<td><jsp:getProperty property="category_id" name="task"/>:<%=category_name %></td>
</tr>
<tr>
<th scope="row">Deadline</th>
<td><jsp:getProperty property="limit_date" name="task"/></td>
</tr>
<tr>
<th scope="row">User name</th>
<td><%=user_name %></td>
</tr>
<tr>
<th scope="row">status</th>
<td><jsp:getProperty property="status_code" name="task"/>:<%=status_name %></td>
</tr>
<tr>
<th scope="row">Note</th>
<td><jsp:getProperty property="memo" name="task"/></td>
</tr>
<tr>
<th scope="row">Registered Date</th>
<td><jsp:getProperty property="create_datetime" name="task"/></td>
</tr>
<tr>
<th scope="row">Update date and time</th>
<td><jsp:getProperty property="update_datetime" name="task"/></td>
</tr>
</tbody>
</table>
<form action="task-delete-servlet" method="post">
<input type="hidden" name="task_id" value="<jsp:getProperty property="task_id" name="task"/>">
<input type="hidden" name="version" value="<%=version %>">
<button type="submit" class="btn btn-outline-danger">Delete</button>
</form>
</div>
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js" integrity="sha384-OgVRvuATP1z7JjHLkuOU7Xw704+h835Lr+6QL9UvYjZE3Ipu6Tp75j7Bh/kR0JKI" crossorigin="anonymous"></script>
</body>
</html>
If the task can be deleted successfully, have it confirmed on this screen.
task-delete-comp.java
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Deletion completed</title>
<link rel="stylesheet" href="${pageContext.request.contextPath}/task-edit-failure.css">
<body>
<jsp:include page="header.jsp"/>
<div class="contain">
<div class="box">
<h3>The task has been deleted</h3>
</div>
</div>
</body>
</html>
Display this screen if task deletion fails (If you cannot delete it for some reason such as exclusive control to be performed next time, we will inform you here)
task-delete-failure.java
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Failed to delete task</title>
<link rel="stylesheet" href="${pageContext.request.contextPath}/task-edit-failure.css">
</head>
<body>
<jsp:include page="header.jsp"/>
<div class="contain">
<div class="box">
<h3>Failed to delete task</h3>
</div>
</div>
</body>
</html>
Here you can also see the methods that appear in the task edit article created last time. Please see only those who need it
model.dao.TaskDAO.java
/**
* task_Get a specific task with id as an argument
* @param task_id
* @return
* @throws SQLException
* @throws ClassNotFoundException
*/
public TaskBean getSpecificTask(int task_id) throws SQLException, ClassNotFoundException {
TaskBean task = new TaskBean();
String sql = "select * from t_task where task_id = ?";
try(Connection con = ConnectionManager.getConnection();
PreparedStatement pstmt = con.prepareStatement(sql);){
pstmt.setInt(1, task_id);
ResultSet res = pstmt.executeQuery();
//Store column information in task object
while(res.next()) {
task.setTask_id(task_id);
task.setTask_name(res.getString("task_name"));
task.setCategory_id(res.getInt("category_id"));
task.setLimit_date(res.getDate("limit_date"));
task.setUser_id(res.getString("user_id"));
task.setStatus_code(res.getString("status_code"));
task.setMemo(res.getString("memo"));
task.setCreate_datetime(res.getTimestamp("create_datetime"));
task.setUpdate_datetime(res.getTimestamp("update_datetime"));
task.setVersion(res.getInt("version"));
}
}
return task;
}
/**
*Method for deleting tasks
* @param task_id
* @return sum
* @throws SQLException
* @throws ClassNotFoundException
*/
public int deleteTask(int task_id) throws SQLException, ClassNotFoundException {
String sql = "delete from t_task where task_id = ?";
int sum = 0;
try(Connection con = ConnectionManager.getConnection();
PreparedStatement pstmt = con.prepareStatement(sql)){
pstmt.setInt(1, task_id);
sum = pstmt.executeUpdate();
}
return sum;
}
Here, get the information of the selected task from the task list screen, put it in the session scope, and transfer it to the confirmation screen.
servlet.TaskDeleteDetailServlet.java
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
TaskDAO taskdao = new TaskDAO();
TaskBean task = new TaskBean();
CategoryDAO categorydao = new CategoryDAO();
StatusDAO statusdao = new StatusDAO();
UserDAO userdao = new UserDAO();
//Get request parameters
request.setCharacterEncoding("UTF-8");
int task_id = Integer.parseInt(request.getParameter("task_id"));
try {
task = taskdao.getSpecificTask(task_id);
int category_id = task.getCategory_id();
String status_code = task.getStatus_code();
String user_id = task.getUser_id();
String category_name = categorydao.getCategoryName(category_id);
String status_name = statusdao.getStatusName(status_code);
String user_name = userdao.getUserName(user_id);
int version = task.getVersion();
request.setAttribute("task", task);
request.setAttribute("category_name", category_name);
request.setAttribute("status_name", status_name);
request.setAttribute("user_name", user_name);
request.setAttribute("version", version);
}catch(SQLException | ClassNotFoundException e) {
e.printStackTrace();
}
RequestDispatcher rd = request.getRequestDispatcher("task-delete.jsp");
rd.forward(request, response);
}
servlet.TaskDeleteServlet.java
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
//Get request parameters
request.setCharacterEncoding("UTF-8");
int task_id = Integer.parseInt(request.getParameter("task_id"));
int version = Integer.parseInt(request.getParameter("version"));
TaskDAO taskdao = new TaskDAO();
List<String> error = new ArrayList<String>();
try{
//Confirm version
int current_version = taskdao.getVersion(task_id);
if(current_version != version) {
error.add("This task has been overwritten by someone else");
request.setAttribute("error", error);
RequestDispatcher rd = request.getRequestDispatcher("task-update-failure.jsp");
rd.forward(request, response);
}
//Delete task execution
taskdao.deleteTask(task_id);
RequestDispatcher rd = request.getRequestDispatcher("task-delete-comp.jsp");
rd.forward(request, response);
} catch(SQLException | ClassNotFoundException e) {
RequestDispatcher rd = request.getRequestDispatcher("task-delete-failure.jsp");
rd.forward(request, response);
}
}
This time, we have implemented the delete function. The minimum functionality for a first-time application can be achieved using the code in the articles so far. Next time, I will introduce the process called exclusive control when multiple users consider using the application at the same time.
Recommended Posts