본문으로 바로가기

url-pattern FrontController패턴

category 프로그래밍/jsp 2018. 1. 2. 22:55


디렉터리 패턴


http://localhost:8181/jsp_21_1_ex1_memberex/Hello ---->/Hello 서블릿

디렉터리 형태로 서버의 해당 컴포넌트를 찾아서 실행하는 구조 입니다



확장자 패턴


확장자 형태로 서버의 해당 컴포넌트를 찾아서 실행하는 구조

http://localhost:8181/jsp_21_1_ex1_memberex/hello.do  --->*.do 서블릿






command클래스

클라이언트로부터 받은 요청들에 대해서, 서블릿이 작업을 직접 처리 하지 않고, 해당 클래스가 처리하도록 합니다.



request.getContextPath()는 프로젝트 path만 얻어온다.

요청 : http://localhost/ZESTINE/test.jsp 경우

→ /ZESTINE 경로만 얻는다


request.getRequestURI()는 프로젝트와 파일 경로까지 얻어온다.

요청 : http://localhost/ZESTINE/test.jsp 경우

→ /ZESTINE/test.jsp 까지 얻어온다.


request.getRequestURI()를 사용한, 파일 이름만 얻어오는 방법(Split)

String[] url = request.getRequestURI().split("/");

String fileName = url[url.length-1]; // 배열의 마지막 값이 파일이름

출처 : Tong - hArMa님의 Java/JSP통





출처: http://luckys.tistory.com/156 [Lucky's...]







frontControllerEx.jsp

<%@ page language="java" contentType="text/html; charset=EUC-KR"

    pageEncoding="EUC-KR"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">

<title>Insert title here</title>

</head>

<body>


<a href="insert.do">insert</a>

<hr />

<a href="http://localhost:8181<%=request.getContextPath()%>/update.do">update</a>

<hr />

<a href="http://localhost:8181/jsp_25_2_ex1_frontex/select.do">select</a>

<hr />

<a href="<%=request.getContextPath()%>/delete.do">delete</a>


FontCon.java


package com.javalec.ex;


import java.io.IOException;


import javax.servlet.ServletException;

import javax.servlet.annotation.WebServlet;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;


/**

 * Servlet implementation class FrontCon

 */

@WebServlet("*.do") //*.do확장자 한곳으로 집중

public class FrontCon extends HttpServlet {

private static final long serialVersionUID = 1L;

       

    /**

     * @see HttpServlet#HttpServlet()

     */

    public FrontCon() {

        super();

        // TODO Auto-generated constructor stub

    }


/**

* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)

*/

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

// TODO Auto-generated method stub

System.out.println("doGet");

actionDo(request, response);

}


/**

* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)

*/

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

// TODO Auto-generated method stub

System.out.println("doPost");

actionDo(request, response);

}

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

// TODO Auto-generated method stub

System.out.println("actionDo");

String uri = request.getRequestURI();  프로젝트 + 이름

System.out.println("uri : " + uri);

String conPath = request.getContextPath(); 프로젝트

System.out.println("conPath : " + conPath);

String command = uri.substring(conPath.length()); uri - conpath를 뺀것

System.out.println("command : " + command);


if(command.equals("/insert.do")){

System.out.println("insert");

System.out.println("----------------");

}else if(command.equals("/update.do")){

System.out.println("update");

System.out.println("----------------");

}else if(command.equals("/select.do")){

System.out.println("select");

System.out.println("----------------");

}else if(command.equals("/delete.do")){

System.out.println("delete");

System.out.println("----------------");

}

}


}



</body>

</html>




'프로그래밍 > jsp' 카테고리의 다른 글

JSTL  (506) 2017.12.31
EL(Expression Language)  (477) 2017.12.19
파일업로드  (484) 2017.12.18
19.데이터베이스-3 (회원가입 및 회원정보 프로그래밍)  (468) 2017.12.09
20. 커넥션풀(DAO,DTO, PrepaaredStatement, 커넥션풀)  (487) 2017.12.08