본문 바로가기
Web Programming/JAVA MVC

FrontController Servlet 생성/ 경로 잘라서 배열로 만들기request.getRequeestURI/request.getContextPath/requst.getRequestURL/requst.Servletpath()

by hyeon-H 2021. 5. 10.
728x90
반응형

MVC패턴에서 FrontController Sevlet 생성하기

 

 

@WebServlet("/FrontController")
public class FrontController extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public FrontController() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		String uri = request.getRequestURI();

		int length = request.getContextPath().length();
		String str = uri.substring(length);

		String[] splitStr = str.split("/");

		String str2 = splitStr[1];

		if(str2.equals("board")) {
			BoardController bc = new BoardController();
			bc.doGet(request, response);
		}
	
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}

 

넘어온 전체 경로중에 도메인 이후의 경로만 가져와서,
프로젝트의 부분만 꺼내어 "/"로 잘라서 배열형태를 만들었다.
[0]은 값이 없고, [1]은 "/"가 빠진 프로젝트만 있다.
그 값이 board이면 BoardController를 생성하고 doGet메소드를 호출하고
Request객체와 Response객체를 호출해 인자로 전달하는 것이다.

 

넘어온 전체경로 http://localhost:8080/board/home.jsp 라고했을때.

request.getRequeestURI() 
프로젝트+파일을 가져옵니다.
도매인이후의 주소를 가져옵니다.
http://localhost:8080/board/home.jsp

request.getContextPath()
프로젝트만 가져옵니다.
http://localhost:8080/board/home.jsp

requst.getRequestURL()
전체 경로를 가져옵니다.
http://localhost:8080/board/home.jsp

requst.Servletpath()
파일만 가져옵니다.
http://localhost:8080/board/home.jsp

 

최족으로 얻어낸 결과가 /board라면 boradController를 생성하고 boradController의 doget()을 호출합니다.

728x90
반응형