Java Servlet 프로그래밍 1차
// Domain
@Getter @Setter
public class Student {
//아이디
private String id;
//이름
private String name;
//성별
private Gender gender;
//나이
private int age;
//생성일
private LocalDateTime createdAt;
}
// Repository
public interface StudentRepository {
//학생-등록
void save(Student student);
//학생-수정
void update(Student student);
//학생-삭제
void deleteById(String id);
//학생-조회_by id
Student getStudentById(String id);
//학생-리스트
List<Student> getStudents();
//학생-아이디 존재여부
boolean existById(String id);
}
// Repository 구현
public class MapStudentRepository implements StudentRepository {
private final Map<String, Student> studentsMap = new ConcurrentHashMap<>();
@Override
public void save(Student student) {
studentsMap.put(student.getId(), student);
}
@Override
public void update(Student student) {
if (studentsMap.containsKey(student.getId())) {
studentsMap.put(student.getId(), student);
} else {
throw new IllegalArgumentException();
}
}
@Override
public void deleteById(String id) {
studentsMap.remove(id);
}
@Override
public Student getStudentById(String id) {
return studentsMap.get(id);
}
@Override
public List<Student> getStudents() {
return new ArrayList<>(studentsMap.values());
}
@Override
public boolean existById(String id) {
return studentsMap.containsKey(id);
}
}
// Servlet 예시 (StudnetUpdateServlet)
@WebServlet(name = "studentUpdateServlet", urlPatterns = "/student/update")
public class StudentUpdateServlet extends HttpServlet {
private StudentRepository studentRepository;
@Override
public void init(ServletConfig config) {
studentRepository = (StudentRepository) config.getServletContext().getAttribute("studentRepository");
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// id 를 받았으니 해당 아이디로 학생을 조회해 jsp 로 넘김
String id = req.getParameter("id");
Student student = studentRepository.getStudentById(id);
req.setAttribute("student", student);
// RequestDispatcher rd = req.getRequestDispatcher("/student/register");
// rd.forward(req, resp);
req.setAttribute("view", "/student/register");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) {
try {
String id = req.getParameter("id");
String name = req.getParameter("name");
Gender gender = Gender.valueOf(req.getParameter("gender"));
int age = Integer.parseInt(req.getParameter("age"));
req.getParameter("age");
if (id == null || name == null) {
log.error("파라미터에 null 이 있음");
resp.sendRedirect("/student/register.jsp");
return;
}
// 기존 학생 정보 id로 가져오기
Student existingStudent = studentRepository.getStudentById(id);
existingStudent.setName(name);
existingStudent.setGender(gender);
existingStudent.setAge(age);
studentRepository.update(existingStudent);
// resp.sendRedirect("/student/view?id=" + id);
req.setAttribute("view","redirect:/student/view.do?id=" + existingStudent.getId());
log.info("update: front 로 가서 처리");
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
스프링으로만 간단하게 해봤어서 쉽게 했었는데 스프링 없이 Servlet 으로 하려니 어렵긴 하다...