MVC

08일차_MVC패턴, 직원관리

알 수 없는 사용자 2015. 6. 30. 14:40

----------------------------
직원관리 Servlet(MVC 패턴) 방식
- 서블릿 주소를 확장자 형태 사용.
- web.xml 파일에는 확장자 형태의 주소 한 개만 등록
- 주소 분석 및 액션 요청을 위한 전용 서블릿 클래스 작성.
- 액션 담당 전용 클래스 작성.

** 환경 설정 추가 -> WEB-INF/source 폴더 하위에 JSP 페이지 등록

10. 소스코드 -> 로그인, 관리자, 직원 전용 페이지


//Controller.java
package com.test;

import java.io.IOException;

import javax.servlet.*;
import javax.servlet.http.*;
import com.sun.corba.se.spi.orbutil.fsm.*;

public class Controller extends HttpServlet {

 private static final long serialVersionUID = 1L;

 @Override
 protected void doGet(HttpServletRequest req, HttpServletResponse resp)
   throws ServletException, IOException {
  doGetPost(req, resp);
 }

 @Override
 protected void doPost(HttpServletRequest req, HttpServletResponse resp)
   throws ServletException, IOException {
  doGetPost(req, resp);
 }

 private void doGetPost(HttpServletRequest request,
   HttpServletResponse response) throws ServletException, IOException {

  // 요청 주소 분석 및 액션 메소드 호출
  String uri = request.getRequestURI();
  // -> "http://localhost:8090/프로젝트이름/Sample.it"

  Action action = new Action();
  String result = "Error.jsp";

  //요청 주소별로 작성할 것.
  if (uri.indexOf("RegionList.it") != -1) {
   // 액션 메소드 호출
   // 메소드 결과는 URI 주소 형태를 가지고 있다.
   result = action.regionList(request, response);
  }
  
  if (uri.indexOf("RegionInsert.it") != -1) {
   result = action.regionInsert(request, response);
  }
  
  if (uri.indexOf("RegionDelete.it") != -1) {
   result = action.regionDelete(request, response);
  }
  
  if (uri.indexOf("RegionUpdate.it") != -1) {
   result = action.regionUpdate(request, response);
  }
  
  if (uri.indexOf("PositionList.it") != -1) {
   // 액션 메소드 호출
   // 메소드 결과는 URI 주소 형태를 가지고 있다.
   result = action.positionList(request, response);
  }
  
  if (uri.indexOf("PositionInsert.it") != -1) {
   result = action.positionInsert(request, response);
  }
  
  if (uri.indexOf("PositionDelete.it") != -1) {
   result = action.positionDelete(request, response);
  }
  
  if (uri.indexOf("PositionUpdate.it") != -1) {
   result = action.positionUpdate(request, response);
  }
  
  if (uri.indexOf("DepartmentList.it") != -1) {
   // 액션 메소드 호출
   // 메소드 결과는 URI 주소 형태를 가지고 있다.
   result = action.departmentList(request, response);
  }
  
  if (uri.indexOf("DepartmentInsert.it") != -1) {
   result = action.departmentInsert(request, response);
  }
  
  if (uri.indexOf("DepartmentDelete.it") != -1) {
   result = action.departmentDelete(request, response);
  }
  
  if (uri.indexOf("DepartmentUpdate.it") != -1) {
   result = action.departmentUpdate(request, response);
  }
  
  
  //문제) 주소 등록
  if (uri.indexOf("EmployeeList.it") != -1) {
   result = action.employeeList(request, response);
  }
  if (uri.indexOf("EmployeeSearch.it") != -1) {
   result = action.employeeSearch(request, response);
  }
  if (uri.indexOf("EmployeeInsertForm.it") != -1) {
   result = action.employeeInsertForm(request, response);
  }
  if (uri.indexOf("EmployeeInsert.it") != -1) {
   result = action.employeeInsert(request, response);
  }
  if (uri.indexOf("PositionReceive.it") != -1) {
   result = action.positionReceive(request, response);
  }
  if (uri.indexOf("EmployeeDelete.it") != -1) {
   result = action.employeeDelete(request, response);
  }
  if (uri.indexOf("EmployeeUpdateForm.it") != -1) {
   result = action.employeeUpdateForm(request, response);
  }
  if (uri.indexOf("EmployeeUpdate.it") != -1) {
   result = action.employeeUpdate(request, response);
  }
  
  
  if (uri.indexOf("LoginForm.it") != -1) {
   result = action.loginForm(request, response);
  }
  if (uri.indexOf("Login.it") != -1) {
   result = action.login(request, response);
  }
  if (uri.indexOf("Logout.it") != -1) {
   result = action.logout(request, response);
  }
  if (uri.indexOf("LogoutForm.it") != -1) {
   result = action.logoutForm(request, response);
  }
  if (uri.indexOf("LoginFail.it") != -1) {
   result = action.loginFail(request, response);
  }
  
  if (uri.indexOf("EmpSearch.it") != -1) {
   result = action.empSearch(request, response);
  }
  
  
  

  // 페이지 전환 -> forward(), sendRedirect() 메소드
  if (result.indexOf("redirect:") != -1) {
   // sendRedirect 요청
   // -> 또 다른 서블릿 주소 요청
   response.sendRedirect(result.substring(9));
  } else {
   // forward 요청
   // -> 연관된 JSP 페이지 요청
   RequestDispatcher dispatcher = request.getRequestDispatcher(result);
   dispatcher.forward(request, response);
  }
 }

}

 

 


//Action.java
package com.test;

import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

//액션 전용 클래스 -> 메소드 단위로 액션 처리
public class Action {

 /*
 public String 메소드이름(HttpServletRequest request, HttpServletResponse response)
  throws ServletException, IOException {
  HttpSession session = request.getSession();

  
  return "WEB-INF/source/요청주소";
  //return "redirect:요청주소";
 }
 */
 
 public String regionList(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  HttpSession session = request.getSession();

 
  //액션 및 결과 출력
  RegionDAO dao = new RegionDAO();
  request.setAttribute("list", dao.regionList());
  
  return "WEB-INF/source/RegionList.jsp";
 }
 
 public String regionInsert(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  HttpSession session = request.getSession();

  //액션 전용
  //데이터 수신(한글 인코딩 추가)
  request.setCharacterEncoding("UTF-8");
  String regionName = request.getParameter("regionName");

  //데이터베이스 액션 -> add() 메소드 호출로 대체
  RegionDAO dao = new RegionDAO();
  Region r = new Region();
  r.setRegionName(regionName);
  dao.add(r);
  return "redirect:RegionList.it";
 }
 
 public String regionDelete(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  HttpSession session = request.getSession();

  //액션 전용
  //데이터 수신(한글 인코딩 추가)
  request.setCharacterEncoding("UTF-8");
  String regionId = request.getParameter("regionId");

  //데이터베이스 액션 -> add() 메소드 호출로 대체
  RegionDAO dao = new RegionDAO();
  dao.remove(regionId);
  
  return "redirect:RegionList.it";
 }
 
 public String regionUpdate(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  HttpSession session = request.getSession();

  //액션 전용
  //데이터 수신(한글 인코딩 추가)
  request.setCharacterEncoding("UTF-8");
  String regionName = request.getParameter("regionName");
  String regionId = request.getParameter("regionId");
  
  //데이터베이스 액션 -> add() 메소드 호출로 대체
  RegionDAO dao = new RegionDAO();
  Region r = new Region();
  r.setRegionName(regionName);
  r.setRegionId(regionId);
  dao.modify(r);
  
  return "redirect:RegionList.it";
 }

 public String positionList(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  HttpSession session = request.getSession();

  //액션 및 결과 출력
  PositionDAO dao = new PositionDAO();
  request.setAttribute("list", dao.positionList());
  
  return "WEB-INF/source/PositionList.jsp";
 }
 
 public String positionInsert(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  HttpSession session = request.getSession();

  //액션 전용
  //데이터 수신(한글 인코딩 추가)
  request.setCharacterEncoding("UTF-8");
  String positionName = request.getParameter("positionName");
  String minBasicPay = request.getParameter("minBasicPay");
  //데이터베이스 액션 -> add() 메소드 호출로 대체
  PositionDAO dao = new PositionDAO();
  Position p = new Position();
  p.setPositionName(positionName);
  p.setMinBasicPay(Integer.parseInt(minBasicPay));
  dao.add(p);
  return "redirect:PositionList.it";
 }
 
 public String positionDelete(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  HttpSession session = request.getSession();

  //액션 전용
  //데이터 수신(한글 인코딩 추가)
  request.setCharacterEncoding("UTF-8");
  String positionId = request.getParameter("positionId");

  //데이터베이스 액션 -> add() 메소드 호출로 대체
  PositionDAO dao = new PositionDAO();
  dao.remove(positionId);
  
  return "redirect:PositionList.it";
 }
 
 public String positionUpdate(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  HttpSession session = request.getSession();

  //액션 전용
  //데이터 수신(한글 인코딩 추가)
  request.setCharacterEncoding("UTF-8");
  String positionName = request.getParameter("positionName");
  String positionId = request.getParameter("positionId");
  String minBasicPay = request.getParameter("minBasicPay");
  //데이터베이스 액션 -> add() 메소드 호출로 대체
  PositionDAO dao = new PositionDAO();
  Position p = new Position();
  p.setPositionName(positionName);
  p.setPositionId(positionId);
  p.setMinBasicPay(Integer.parseInt(minBasicPay));
  dao.modify(p);
  
  return "redirect:PositionList.it";
 }

 public String departmentList(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  HttpSession session = request.getSession();

  //액션 및 결과 출력
  DepartmentDAO dao = new DepartmentDAO();
  request.setAttribute("list", dao.departmentList());
  
  return "WEB-INF/source/DepartmentList.jsp";
 }
 
 public String departmentInsert(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  HttpSession session = request.getSession();

  //액션 전용
  //데이터 수신(한글 인코딩 추가)
  request.setCharacterEncoding("UTF-8");
  String departmentName = request.getParameter("departmentName");

  //데이터베이스 액션 -> add() 메소드 호출로 대체
  DepartmentDAO dao = new DepartmentDAO();
  Department d = new Department();
  d.setDepartmentName(departmentName);
  dao.add(d);
  return "redirect:DepartmentList.it";
 }
 
 public String departmentDelete(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  HttpSession session = request.getSession();

  //액션 전용
  //데이터 수신(한글 인코딩 추가)
  request.setCharacterEncoding("UTF-8");
  String departmentId = request.getParameter("departmentId");

  //데이터베이스 액션 -> add() 메소드 호출로 대체
  DepartmentDAO dao = new DepartmentDAO();
  dao.remove(departmentId);
  
  return "redirect:DepartmentList.it";
 }
 
 public String departmentUpdate(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  HttpSession session = request.getSession();

  //액션 전용
  //데이터 수신(한글 인코딩 추가)
  request.setCharacterEncoding("UTF-8");
  String departmentName = request.getParameter("departmentName");
  String departmentId = request.getParameter("departmentId");
  
  //데이터베이스 액션 -> add() 메소드 호출로 대체
  DepartmentDAO dao = new DepartmentDAO();
  Department d = new Department();
  d.setDepartmentName(departmentName);
  d.setDepartmentId(departmentId);
  dao.modify(d);
  
  return "redirect:DepartmentList.it";
 }
 
 
 
 public String employeeList(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
 
  HttpSession session = request.getSession();

  //문제) 데이터베이스 액션 및 결과 데이터 전달
  EmployeeDAO dao = new EmployeeDAO();
  request.setAttribute("count", dao.count());
  request.setAttribute("list", dao.list());
  
  return "WEB-INF/source/EmployeeList.jsp";
 }
 
 public String employeeSearch(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
 
  HttpSession session = request.getSession();

  //문제) 데이터 수신 및 데이터베이스 액션, 결과 데이터 전달
  request.setCharacterEncoding("UTF-8");
  String searchKey = request.getParameter("searchKey");
  String searchValue = request.getParameter("searchValue");
  if (searchKey != null) {
   EmployeeDAO dao = new EmployeeDAO();
   request.setAttribute("count", dao.count(searchKey, searchValue));
   request.setAttribute("list", dao.list(searchKey, searchValue));
  }
  
  request.setAttribute("searchKey", searchKey);
  request.setAttribute("searchValue", searchValue);
  
  return "WEB-INF/source/EmployeeSearch.jsp";
 }
 
 
 
 public String employeeInsertForm(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  
  HttpSession session = request.getSession();

  EmployeeDAO dao = new EmployeeDAO();
  
  request.setAttribute("positionList", dao.positionList());
  request.setAttribute("regionList", dao.regionList());
  request.setAttribute("departmentList", dao.departmentList());
  
  return "WEB-INF/source/EmployeeInsertForm.jsp";
 }
 
 public String employeeInsert(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  
  HttpSession session = request.getSession();

  //데이터 수신
  request.setCharacterEncoding("UTF-8");
  String name = request.getParameter("name");
  String ssn = request.getParameter("ssn");
  String birthday = request.getParameter("birthday");
  String lunar = request.getParameter("lunar");
  String telephone = request.getParameter("telephone");
  String regionId = request.getParameter("regionId");
  String departmentId = request.getParameter("departmentId");
  String positionId = request.getParameter("positionId");
  String basicPay = request.getParameter("basicPay");
  String extraPay = request.getParameter("extraPay");

  //데이터베이스 액션 -> add() 메소드 호출
  EmployeeDAO dao = new EmployeeDAO();
  Employee e = new Employee();
  e.setName(name);
  e.setSsn(ssn);
  e.setBirthday(birthday);
  e.setLunar(Integer.parseInt(lunar));
  e.setTelephone(telephone);
  e.setRegionId(regionId);
  e.setDepartmentId(departmentId);
  e.setPositionId(positionId);
  e.setBasicPay(Integer.parseInt(basicPay));
  e.setExtraPay(Integer.parseInt(extraPay));
  dao.add(e);
  
  return "redirect:EmployeeList.it";
 }
 
 public String positionReceive(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  
  HttpSession session = request.getSession();

  //페이지 차단 액션 추가 (관리자 전용 페이지)

  
  //데이터 수신 (positionId)
  String positionId = request.getParameter("positionId");

  EmployeeDAO dao = new EmployeeDAO();
  request.setAttribute("result", String.valueOf(dao.getMinBasicPay(positionId)));
  
  return "WEB-INF/source/PositionReceive.jsp";
 }
 
 
 public String employeeDelete(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
   
  HttpSession session = request.getSession();
  
  //페이지 차단 액션 추가 (관리자 전용 페이지)

  
  request.setCharacterEncoding("UTF-8");
  String employeeId = request.getParameter("employeeId");

  EmployeeDAO dao = new EmployeeDAO();
  dao.remove(employeeId);
 
  
  return "redirect:EmployeeList.it";
 }
 
 public String employeeUpdateForm(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  
  HttpSession session = request.getSession();

  //페이지 차단 액션 추가 (관리자 전용 페이지)

  //데이터 수신 -> emploeeId
  //데이터베이스 액션 -> list("employeeId",value) 메소드

  EmployeeDAO dao = new EmployeeDAO();
  
  String employeeId = request.getParameter("employeeId");
  
  request.setAttribute("emp", (dao.list("0", employeeId)).get(0));
  
  request.setAttribute("positionList", dao.positionList());
  request.setAttribute("departmentList",dao.departmentList());
  request.setAttribute("regionList",dao.regionList());
  
  return "WEB-INF/source/EmployeeUpdateForm.jsp";
 }
 public String employeeUpdate(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  HttpSession session = request.getSession();

  //페이지 차단 액션 추가 (관리자 전용 페이지)
  
  request.setCharacterEncoding("UTF-8");
  
  //데이터 수신
  String employeeId = request.getParameter("employeeId");
  String name = request.getParameter("name");
  String ssn = request.getParameter("ssn");
  String birthday = request.getParameter("birthday");
  String lunar = request.getParameter("lunar");
  String telephone = request.getParameter("telephone");
  String regionId = request.getParameter("regionId");
  String departmentId = request.getParameter("departmentId");
  String positionId = request.getParameter("positionId");
  String basicPay = request.getParameter("minBasicPay");
  String extraPay = request.getParameter("extraPay");
  String grade = request.getParameter("grade");
  
  //DB 액션  -> modify() 메소드 호출
  Employee e = new Employee();
  e.setEmployeeId(employeeId);
  e.setName(name);
  e.setSsn(ssn);
  e.setBirthday(birthday);
  e.setLunar(Integer.parseInt(lunar));
  e.setTelephone(telephone);
  e.setRegionId(regionId);
  e.setDepartmentId(departmentId);
  e.setPositionId(positionId);
  e.setBasicPay(Integer.parseInt(basicPay));
  e.setExtraPay(Integer.parseInt(extraPay));
  e.setGrade(Integer.parseInt(grade));

  EmployeeDAO dao = new EmployeeDAO();
  
  dao.modify(e);
  
  return "redirect:EmployeeList.it";
 }
 
 public String loginForm(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  
  return "WEB-INF/source/LoginForm.jsp";
 }

 public String login(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  HttpSession session = request.getSession();

  //문제)
  //데이터 수신 -> id, pw, grade
  
  //데이터베이스 액션->로그인 검증
  
  //로그인 실패-> LoginFail.it
  //로그인 성공->관리자->EmployeeList.it
  //로그인 성공->일반직원->EmpSearch.it

  return "LoginFail.it";
 }

 public String logout(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  HttpSession session = request.getSession();
  
  //문제)
  //세션 객체 소멸

  
  
  
  return "redirect:LogoutForm.it";
 }

 public String logoutForm(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  
  return "WEB-INF/source/LogoutForm.jsp";
 }

 public String loginFail(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  
  return "WEB-INF/source/LoginFail.jsp";
 }

 
 public String empSearch(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  HttpSession session = request.getSession();
  
  //페이지 차단 액션 추가 (일반 직원용 페이지)
  if (session.getAttribute("login") == null) {
   response.sendRedirect("LoginForm.it");
  }
  
  
  //문제)
  //데이터 수신
  //데이터베이스 액션

  return "WEB-INF/source/EmpSearch.jsp";
 }
 
}

 

 


//EmployeeMenu.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
  <p>
   [<a href="EmployeeList.it">직원관리</a>]
   [<a href="RegionList.it">지역관리</a>]
   [<a href="DepartmentList.it">부서관리</a>]
   [<a href="PositionList.it">직위관리</a>]
   <!-- 로그인, 로그아웃 관련 메뉴 추가 예정 -->
   [<a href="Logout.it">로그아웃(${sessionScope.login})</a>]
  </p>

 


//EmpMenu.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
  <p>
   <!-- 로그인, 로그아웃 관련 메뉴 추가 예정 -->
   [<a href="Logout.it">로그아웃(${sessionScope.login})</a>]
  </p>

 


---------------------------------------