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
This time, we will create an edit page. It's very similar to the task registration screen I wrote in the previous article, so please check it easily. For task editing, logged-in users can edit tasks other than those posted by themselves.
task-update.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Task editing</title>
<link rel="stylesheet" href="${pageContext.request.contextPath}/task-insert.css">
</head>
<body>
<jsp:include page="header.jsp"/>
<jsp:useBean id="task" scope="session" class="model.entity.TaskBean"/>
<div class="contain">
<h3>Task registration</h3>
<form action="task-update-servlet" method="post">
<table class="form-table" border="1">
<tbody>
<tr>
<th>Task name</th>
<td>
<div class="input-key">
<input type="text" class="form-control" name="task_name" value="<jsp:getProperty property="task_name" name="task"/>">
</div>
</td>
</tr>
<tr>
<th>Category</th>
<td>
<div class="input-key">
<select class="form-control" name="category_id">
<option value="<jsp:getProperty property="category_id" name="task"/>">No change</option>
<option value="1">New product A: Development project</option>
<option value="2">Existing product B:Improvement project</option>
</select>
</div>
</td>
</tr>
<tr>
<th>Deadline</th>
<td>
<div class="input-key">
<input type="date" name="limit_date" class="form-control" value="<jsp:getProperty property="limit_date" name="task"/>">
</div>
</td>
</tr>
<tr>
<th>status</th>
<td>
<div class="input-key">
<select class="form-control" name="status_code">
<option value="<jsp:getProperty property="status_code" name="task"/>">No change</option>
<option value="00">not started yet</option>
<option value="50">Start</option>
<option value="99">Done</option>
</select>
</div>
</td>
</tr>
<tr>
<th>Note</th>
<td>
<div class="input-key">
<input type="text" class="form-control" name="memo" value="<jsp:getProperty property="memo" name="task"/>">
</div>
</td>
</tr>
<tr>
<th>Created date</th>
<td><jsp:getProperty property="create_datetime" name="task"/><input type="hidden" name="create_datetime" value="<jsp:getProperty property="create_datetime" name="task"/>"></td>
</tr>
<tr>
<th>
<input type="hidden" name="task_id" value="<jsp:getProperty property="task_id" name="task"/>">
<input type="hidden" name="version" value='<jsp:getProperty property="version" name="task"/>'>
<input type="submit" value="Update" class="input-submit"></th>
<td></td>
</tr>
</tbody>
</table>
</form>
</div>
</body>
</html>
In addition, we will prepare a screen when editing is successful. The edited contents are displayed here.
task-update-comp.java
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" import="model.entity.TaskBean"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Update completed</title>
<link rel="stylesheet" href="${pageContext.request.contextPath}/task-edit-failure.css">
</head>
<body>
<jsp:useBean id="task" class="model.entity.TaskBean" scope="session"/>
<%
String category_name = (String)request.getAttribute("category_name");
String status_name = (String)request.getAttribute("status_name");
%>
<jsp:include page="header.jsp"/>
<div class="contain">
<div class="box">
<h3>Task update is complete</h3>
<p>Updated with the following contents</p>
<hr>
<p>
Task name:<jsp:getProperty property="task_name" name="task"/><br>
Category:<jsp:getProperty property="category_id" name="task"/>:<%=category_name %><br>
Deadline:<jsp:getProperty property="limit_date" name="task"/><br>
status:<jsp:getProperty property="status_code" name="task"/>:<%=status_name %><br>
Note:<jsp:getProperty property="memo" name="task"/><br>
</p>
</div>
</div>
</body>
</html>
Also prepare a screen when it fails Display the reason for failure
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" import="java.util.List"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Task update failure</title>
<link rel="stylesheet" href="${pageContext.request.contextPath}/task-edit-failure.css">
</head>
<body>
<%
List<String> error = (List<String>)request.getAttribute("error");
%>
<jsp:include page="header.jsp"/>
<div class="contain">
<div class="box">
<h3>Failed to update task</h3>
<hr>
<p>*The following causes are possible</p>
<ul>
<%for(String er : error){ %>
<li><%=er %></li>
<%} %>
</ul>
</div>
</div>
</body>
</html>
Create a method to update the task and a method to get the specified task
model.dao.TaskDAO.java
/**
*Method for updating tasks
* @param task
* @return sum
* @throws SQLException
* @throws ClassNotFoundException
*/
public int updateTask(TaskBean task) throws SQLException, ClassNotFoundException {
String sql = "update t_task set task_name = ?, category_id = ?, limit_date = ?, status_code = ?, memo = ?, version = version + 1 where task_id = ? and version = ?";
int sum = 0;
try(Connection con = ConnectionManager.getConnection();
PreparedStatement pstmt = con.prepareStatement(sql)){
pstmt.setString(1, task.getTask_name());
pstmt.setInt(2, task.getCategory_id());
pstmt.setDate(3, task.getLimit_date());
pstmt.setString(4, task.getStatus_code());
pstmt.setString(5, task.getMemo());
pstmt.setInt(6, task.getTask_id());
pstmt.setInt(7, task.getVersion());
sum = pstmt.executeUpdate();
}
return sum;
}
/**
* 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;
}
Save the task information selected from the list screen to the session scope and display / edit it at the transfer destination (task-update.java).
servlet.TaskUpdateDetailServlet.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"));
TaskDAO dao = new TaskDAO();
TaskBean task = new TaskBean();
try {
//Save specified task to session scope
task = dao.getSpecificTask(task_id);
HttpSession session = request.getSession();
session.setAttribute("task", task);
RequestDispatcher rd = request.getRequestDispatcher("task-update.jsp");
rd.forward(request, response);
}catch(SQLException | ClassNotFoundException e) {
e.printStackTrace();
}
After editing, DB processing and screen transition are performed with the following servlet.
servlet.TaskUpdateServket.java
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
TaskDAO taskdao = new TaskDAO();
CategoryDAO categorydao = new CategoryDAO();
StatusDAO statusdao = new StatusDAO();
//Get request parameters
request.setCharacterEncoding("UTF-8");
int task_id = Integer.parseInt(request.getParameter("task_id"));
String task_name = request.getParameter("task_name");
String category_id_str = request.getParameter("category_id");
String limit_date_check = request.getParameter("limit_date");
String status_code = request.getParameter("status_code");
String memo = request.getParameter("memo");
int version = Integer.parseInt(request.getParameter("version"));
//List generation to save error display
List<String> error = new ArrayList<String>();
//session check
HttpSession session = request.getSession();
boolean sessioncheck = (boolean)session.getAttribute("login");
if(!sessioncheck) {
error.add("Please log in before registering the task");
}
//Check for input omissions
if(task_name.equals("")) {
error.add("The task name is blank");
}
boolean limit_check;
limit_date_check = limit_date_check.replace('-', '/');
DateFormat format = DateFormat.getDateInstance();
format.setLenient(false);
try {
format.parse(limit_date_check);
limit_check = true;
} catch(Exception e) {
limit_check = false;
}
if(!limit_check) {
error.add("Please enter the date for the deadline");
}
request.setAttribute("error", error);
if(task_name != "") {
try {
//Convert categories and deadlines (non-String type) that cannot be received according to the parameters
int category_id = Integer.parseInt(category_id_str);
Date limit_date = Date.valueOf(request.getParameter("limit_date"));
//Collate the current version with the version at the time of record acquisition
int current_version = taskdao.getVersion(task_id);
if(current_version != version) {
error.add("This task has been reorganized by others");
request.setAttribute("error", error);
RequestDispatcher rd = request.getRequestDispatcher("task-update-failure.jsp");
rd.forward(request, response);
}
//Set value for task object
TaskBean task = new TaskBean();
task.setTask_name(task_name);
task.setCategory_id(category_id);
task.setLimit_date(limit_date);
task.setStatus_code(status_code);
task.setMemo(memo);
task.setTask_id(task_id);
task.setVersion(version);
//Database processing by update method
taskdao.updateTask(task);
//Save task to session scope
session.setAttribute("task", task);
//Save category name and status name to request scope
String category_name = categorydao.getCategoryName(task.getCategory_id());
String status_name = statusdao.getStatusName(task.getStatus_code());
request.setAttribute("category_name", category_name);
request.setAttribute("status_name", status_name);
RequestDispatcher rd = request.getRequestDispatcher("task-update-comp.jsp");
rd.forward(request, response);
} catch(SQLException | ClassNotFoundException | IllegalArgumentException e) {
RequestDispatcher rd = request.getRequestDispatcher("task-update-failure.jsp");
rd.forward(request, response);
}
}else {
RequestDispatcher rd = request.getRequestDispatcher("task-update-failure.jsp");
rd.forward(request, response);
}
}
This time, I implemented the task editing function. Next time, we will implement the task deletion function.
Recommended Posts