EmployeeController servlet ์์ฑ

doGET, doPost์ ์ธ ์ง์
doGET
package webapp.controller;
import java.io.IOException;
import java.util.List;
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 webapp.dao.EmployeeDAO;
import webapp.model.Employee;
import webapp.model.EmployeeDAOImple;
import webapp.util.DBConn;
@WebServlet("/EmployeeController")
public class EmployeeController extends HttpServlet {
private static final long serialVersionUID = 1L;
EmployeeDAO employeeDAO = null;
// ์์ฑ์
public EmployeeController() {
// ์ปจํธ๋กค๋ฌ ์ฒ์ ์คํ๋์๋ ํ๋ฒ๋ง ๊ฐ์ฒด ๋ง๋ค๋ฉด ๋ผ์ ์์ฑ์์ ๋ฃ์
employeeDAO = new EmployeeDAOImple();// ๊ฐ์ฒด์์ฑ
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String action = request.getParameter("action");
if (action == null) {
action = "LIST";
}
switch (action) {
case "LIST":// action์ด LIST๋ฉด ์ง์๋ฆฌ์คํธ ์ถ๋ ฅ
listEmployee(request, response);
break;
case "EDIT":// action์ด EDIT๋ฉด ์ง์๋ฆฌ์คํธ ์์
getSingleEmployee(request, response);
break;
case "DELETE":// action์ด DELETE๋ฉด ์ง์๋ฆฌ์คํธ ์ญ์
deleteEmployee(request, response);
break;
default:
listEmployee(request, response);
break;
}
}
//๋ฉ์๋ ์์ฑ
private void deleteEmployee(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String id = request.getParameter("id");
if (employeeDAO.delete(Integer.parseInt(id))) {
request.setAttribute("NOTIFICATION", "์ฑ๊ณต์ ์ผ๋ก ์ญ์ ๋จ!");
}
listEmployee(request, response);// ์์ธ์ฒ๋ฆฌ throw์ถ๊ฐ
}
private void getSingleEmployee(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String id = request.getParameter("id");
Employee emp = employeeDAO.get(Integer.parseInt(id));
request.setAttribute("employee", emp);
// ์์ธ์ฒ๋ฆฌ throw์ถ๊ฐ , ์์ ํ๊ธฐ ์ํด์ ์์ ํ์ด์ง๋ก forwardํ๋ค
request.getRequestDispatcher("/views/employee-form.jsp").forward(request, response);
}
private void listEmployee(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
List<Employee> empList = employeeDAO.get();
request.setAttribute("list", empList);
// ์์ธ์ฒ๋ฆฌ throw์ถ๊ฐ , ์ง์๋ชฉ๋ก ํ์ด์ง๋ก forwardํ๋ค.
request.getRequestDispatcher("/views/employee-list.jsp").forward(request, response);
}
doPost ์ถ๊ฐ
// employee-form ์์ submintํ๋ฉด ๋์ด์ด
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String id = request.getParameter("id");
Employee e = new Employee();
e.setName(request.getParameter("name"));
e.setDepartment(request.getParameter("department"));
e.setDob(request.getParameter("dob"));
// ์๋ก์
๋ ฅ vs ์์ => id๊ฐ ์์ผ๋ฉด ์์ ์ด๋ค.
if (id.isEmpty() || id == null) {
// id๊ฐ ์๊ฑฐ๋ null์ด๋ฉด ์
๋ ฅ
if (employeeDAO.save(e)) {
request.setAttribute("NOTIFICATION", "์ง์ ์
๋ ฅ ์ฑ๊ณต!");
}
} else {
// update
e.setId(Integer.parseInt(id));
if (employeeDAO.update(e)) {
request.setAttribute("NOTIFICATION", "์
๋ฐ์ดํธ ์ฑ๊ณต!");
}
}
// ์ง์ ๋ชฉ๋ก ํ๋ฉด์ผ๋ก.
listEmployee(request, response);
}
}
WebContent -> views ํด๋ ์์ฑ
-> employee-form ๋ฐ employee-list jspํ์ผ ์์ฑ

lib -> jstl-1.2์ถ๊ฐ
web.xml
๊ธฐ๋ณธํ์ด์ง ์ค์ ๋ฐ ํ๊ธํํฐ ์ถ๊ฐ
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0">
<display-name>DATABASE</display-name>
<welcome-file-list>
<welcome-file>EmployeeController</welcome-file>
</welcome-file-list>
<!-- ํ๊ธ์ค์ ํํฐ์ถ๊ฐ -->
<filter>
<filter-name>Set Character Encoding</filter-name>
<filter-class>org.apache.catalina.filters.SetCharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Set Character Encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- ํ๊ธ์ค์ END -->
</web-app>
employee-form.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>Insert title here</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
</head>
<body>
<div class = "container">
<h1>Employee Directory</h1>
<hr/>
<div class = "row">
<div class = "col-md-4">
<form action = "${pageContext.request.contextPath}/EmployeeController" method="POST">
<div class = "form-group">
<input type = "text" class = "form-control" name = "name" placeholder = "Enter Name" value = "${employee.name}"/>
</div>
<div class = "form-group">
<input type = "date" class = "form-control" name = "dob" value = "${employee.dob}"/>
</div>
<div class="form-group">
<input type = "text" class = "form-control" name = "department" placeholder = "Enter Department" value = "${employee.department}"/>
</div>
<input type = "hidden" name = "id" value = "${employee.id}"/>
<button type = "submit" class = "btn btn-primary">Save</button>
</form>
</div>
</div>
<a href = "${pageContext.request.contextPath}/EmployeeController?action=LIST">Back to List</a>
</div>
</body>
</html>
employee-list jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
<head>
<title>Employee Directory</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
</head>
<body>
<div class = "container">
<h1>Employee Directory</h1>
<hr/>
<p>${NOTIFICATION}</p>
<p>
<button class = "btn btn-primary" onclick="window.location.href = 'views/employee-form.jsp'">์ง์ ์ถ๊ฐ</button>
</p>
<table class = "table table-striped table-bordered">
<tr class = "thead-dark">
<th>Name</th>
<th>Department</th>
<th>Date of birth</th>
<th>Actions</th>
</tr>
<c:forEach items="${list}" var="employee">
<tr>
<td>${employee.name}</td>
<td>${employee.department}</td>
<td>${employee.dob}</td>
<td>
<a href = "${pageContext.request.contextPath}/EmployeeController?action=EDIT&id=${employee.id}">Edit</a>
|
<a onclick="return confirm('์ญ์ ํ๊ฒ ์ต๋๊น?')" href = "${pageContext.request.contextPath}/EmployeeController?action=DELETE&id=${employee.id}" >Delete</a>
</td>
</tr>
</c:forEach>
</table>
</div>
</body>
</html>

http://localhost:8080/db/

Deltet ์ ์ปค์ ๊ฐ์ ธ๋ค ๋๋ฉด ์ผ์ชฝํ๋จ์

์ง์ ์ ๋ณด ์ถ๊ฐ -> ์ง์ ์ถ๊ฐ
์ผ์ชฝ ์๋จ ์ง์ ์ถ๊ฐ ํด๋ฆญ



์ถ๊ฐ๋ ์ง์ ์ ๋ณด MySQL DBํ์ธ

์ง์ ์ ๋ณด ์์ (update) -> Edit

์์


์์ ๋ ์ง์ ์ ๋ณด MySQL DBํ์ธ

์ง์์ ๋ณด ์ญ์ ํ๊ธฐ(delete)


์ญ์ ๋ ์ง์ ์ ๋ณด MySQL DBํ์ธ

<์ ๋ฆฌ>
โemployee-form :"${pageContext.request.contextPath}/EmployeeController" method="POST"
-> ${pageContext.request.contextPath}
: context path(๊ธฐ๋ณธ url๊ฒฝ๋ก)๊ฐ ๋ณ๊ฒฝ๋ผ๋ ๊ทธ ๋ณ๊ฒฝ๋ ์ฃผ์๋ฅผ ์ฌ์ฉํ๋ค. ๋ง์ฝ context path๊ฐ ์์ ๋ผ๋ ๊ฑด๋ค ํ์์์.
-> method="POST"
EmployeeController์ doPost๋ก ๋ฐ์ดํฐ ์ ์ก
์ง์ ๊ฐ์ฒด ์์ฑ ํ
๊ฐ์ฒด์ ์ด๋ฆ, ๋ถ์๋ช , ์๋ ์์ผ ๊ฐ์ ธ์์ ์ถ๊ฐํ๋ค.
๋ง์ฝ id๊ฐ ๊ณต๋ฐฑ์ด๊ฑฐ๋ null๊ฐ์ด๋ผ๋ฉด ์ง์ ์ ๋ณด๋ฅผ ์ถ๊ฐํ ๋ค์ "์ง์ ์ ๋ ฅ ์ฑ๊ณต!" ๋ฉ์ธ์ง ์ถ๋ ฅ
๋ง์ฝ id๊ฐ ๊ณต๋ฐฑ์ด ์๋๋ผ๋ฉด ํด๋น ์์ด๋์ ์ง์ ์ ๋ณด๋ฅผ ์ ๋ฐ์ดํธํ๊ณ , "์ ๋ฐ์ดํธ ์ฑ๊ณต " ๋ฉ์์ง ์ถ๋ ฅํ๋ค.
'DB > MySQL' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| ๋ฐ์ดํฐ๋ฒ ์ด์ค (์คํค๋ง) ํ ํ์ผ๋ก ๋ฌถ์ด ๋ด๋ณด๋ด๊ธฐ export (0) | 2023.09.27 |
|---|---|
| Simple DB ํ๋ก์ ํธ-3 (1) | 2023.09.21 |
| Simple DB ํ๋ก์ ํธ (0) | 2023.09.21 |