mvc-DB접속
커맨드의 객체화.........??
MVC : model, view, Controller
model - Beans, dao
view - jsp
Controller - java
MVC는 Beans를 거치지않고
Controller 에서 model 를 바로 불러와 쓴다
---------------------------------------------------------------------
package student;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class StudentServlet extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doService(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doService(req, resp);
}
protected void doService(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("EUC-KR");
resp.setContentType("text/html; charset=EUC-KR");
String cmd = req.getParameter("command");
PrintWriter out = resp.getWriter();
String nextPage = null;
if (cmd.equals("insert")) {
}else if (cmd.equals("delete")) {
String id = req.getParameter("id");
StudentDAO dao = new StudentDAO();
int res = 0;
try{
res = dao.deleteStudent(id);
}catch(SQLException e) {
System.err.println("delete 메소드 실행 중 오류 발생!!");
e.printStackTrace();
}
String msg = null, url = null;
if (res>0) {
msg = "학생삭제 성공!! 학생목록페이지로 이동합니다.";
url = "student.do?command=list";
}else {
msg = "학생삭제 실패!! 학생관리페이지로 이동합니다.";
url = "student.do?command=start";
}
req.setAttribute("msg", msg);
req.setAttribute("url", url);
nextPage = "message.jsp";
}else if (cmd.equals("find")) {
String name = req.getParameter("name");
StudentDAO dao = new StudentDAO();
List<StudentDTO> find = null;
try {
find = dao.findStudent(name);
}catch(SQLException e) {
System.err.println("find 메소드 실행 중 오류 발생!!");
e.printStackTrace();
}
req.setAttribute("list", find);
nextPage = "list.jsp";
}else if (cmd.equals("list")) {
StudentDAO dao = new StudentDAO();
List<StudentDTO> list = null;
try{
list = dao.listStudent();
}catch(SQLException e) {
System.err.println("list 메소드 실행 중 오류 발생!!");
e.printStackTrace();
}
req.setAttribute("list", list);
nextPage = "list.jsp";
}else if (cmd.equals("start")) {
nextPage = "student.jsp";
}
System.out.println(cmd);
RequestDispatcher view = req.getRequestDispatcher(nextPage);
view.forward(req, resp);
}
}
---------------------------------------------------------------------------'
하..............................