컨트롤러, 커맨드 구조 추가하여 연동

This commit is contained in:
mcutegs2
2020-06-08 16:50:52 +09:00
parent 70c7c4771a
commit 831a4a2d5e
7 changed files with 125 additions and 10 deletions

View File

@@ -0,0 +1,52 @@
package controller;
import java.io.IOException;
import java.util.HashMap;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
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 command.Command;
import command.HomeCommand;
import command.LoginCommand;
@WebServlet("/Fcontroller")
public class Fcontroller extends HttpServlet {
private static final long serialVersionUID = 1L;
HashMap<String, Command> list = null;
public Fcontroller() {
super();
// TODO Auto-generated constructor stub
}
public void init(ServletConfig config) throws ServletException {
// TODO Auto-generated method stub
list = new HashMap<String, Command>();
list.put("/login.do", new LoginCommand()); //로그인 처리
list.put("/home.do", new HomeCommand()); //처음 보여주는 페이지;
//이 부분에 계속적으로 매핑을 추가하면 됨
}
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
request.setCharacterEncoding("utf-8");
String uri = request.getRequestURI();
String contextPath = request.getContextPath();
String path = uri.substring(contextPath.length());
Command comm = list.get(path);
String view = comm.exec(request, response);
RequestDispatcher dispatcher = request.getRequestDispatcher(view);
dispatcher.forward(request, response);
}
}