DBCP / Filter

DeadWhale·2022년 5월 2일
0

Servlet/JSP

목록 보기
7/22
post-thumbnail

미리 DB와 연결되어 있는 Connection을 일정 개수 만들어 두고
클라이언트 요청 시 만들어둔 Connection을 빌려주고
요청 처리 완료시 다시 반환받는 기법.


[장점]
1) 미리 Connection을 만들어 두기 때문에 요청 시 속도가 빠르다
(이 전에는 그때 그때 만들어서 오래 걸린다.)
2) Connection 갯수에 제한을 두어 DB에 과도하게 요청되는 경우를 방지한다.

Server >Context .xml

<!-- DBCP 세팅 -->   
  <Resource 
     name="jdbc/oracle" 
     auth="Container"
     type="javax.sql.DataSource" 
     driverClassName="oracle.jdbc.OracleDriver"
     url="jdbc:oracle:thin:@127.0.0.1:1521:xe"
     username="community_khg"
     password="community1234" 
     maxTotal="50"      
     maxIdle="10"
     maxWaitMillis="-1"
   />
<!-- 
auth : 자원 관리 주체 지정(Application 또는 Container)
type : Resource 의 타입 지정
driverClassName : JDBC 드라이버 클래스 이름.
maxTotal : DataSource 에서 유지할 수 있는 최대 커넥션 수    
maxIdle : 사용되지 않고 풀에 저장될 수 있는 최대 커넥션의 개수. 음수일 경우 제한이 없음
maxWaitMillis : 커넥션 반납을 기다리는 시간(-1은 반환될 때 까지 기다림)
-->
//JNDI (Java Naming and Directory Interface API)
//-	디렉토리에 접근하는데 사용하는 JAVA API
//- 애플리케이션(프로그램 ,웹앱)은 JNDI를 이용해서 파일 또는 서버 Resource를 찾을 수 있다.

Context initContext = new InitialContext();

//servers - > Context.xml 파일 찾기
Context envContext = (Context)initContext.lookup("java:/comp/env");

//DBCP 세팅의 <Resource> 태그 찾아 DataSource 형식의 객체로 얻어오기
//DataSource : Connection pool을 구현하는 객체(만들어둔 Connection 얻어오기 가능)
DataSource ds = (DataSource)envContext.lookup("jdbc/oracle");
			
conn=ds.getConnection();
conn.setAutoCommit(false);

필터(Filter)

클라이언트 요청시 생성되는
HttpServletRequest, HttpServletResponse 가
요청을 처리하는 Servlet에 도달하기 전에
특정 코드를 수행하는 클래스.

흐름


@WebFilter(filterName = "encodingFilter", urlPatterns = "/*") //이 필터의 이름 지정
public class EncodingFilter extends HttpFilter implements Filter {

		
//서버 실행 시 또는 필터 코드 변경 시 필터가 자동 생성되는데
//그 때 필터에 필요한 내용을 초기화 하는 메서드
public void init(FilterConfig fConfig) throws ServletException {
			System.out.println("문자 인코딩 필터 초기화");
		}
	
//서버 실행 중 필터 코드가 변경되어 기존 필터를 없애야 할 때 수행되는 메서드
public void destroy() {
System.out.println("문자 인코딩 필터 파괴!!!!");
}
	
//실질적으로 필터 역할을 수행하는 메서드
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {

//모든 요청의 문자 인코딩을 UTF-8로 설정
request.setCharacterEncoding("UTF-8");

//모든 응답의 문자 인코딩을 UTF-8로 설정
response.setCharacterEncoding("UTF-8");
				
//연결된 다음 필터 수행(없을 경우 Servlet 수행)
chain.doFilter(request, response);
		}
}

나중에는 암호화등 보안이에 사용 할 수 있다.

여러개의 필터를 체인(chain)으로 엮으면서 순차적으로 진행 할 수 있다.

@WebFilter(filterName = "encodingFilter", urlPatterns = "/*") 

filterName : 말 그대로 필터의 이름을 지정하는 하는 속성 필터의 순서 저장 시에 사용한다.


urlPatterns : 요청 받은 서블릿의 주소를 입력
/ : 최상위 주소를 의미한다.
/* 모든 종류의 주소 요청을 포함한다.

하나의 필터에는 init , destroy ,diFilter 3개로 구성된다

0개의 댓글