Filter
Servlet에서 사용
- 요청발생시 Servlet 진입 전에 필터사용
- 응답발생시 필터를 거쳐서 응답 발생
- URL 패턴으로 필터를 구분
- 여러개의 필터 사용 가능
- web.xml에 등록 된 순서대로 필터 사용
1. 생성
File -> new -> other -> Filter 검색
2. 주요 메서드
1) destroy : Filter 객체가 소멸될 때 호출되는 메서드
2) init... : Filter 객체가 생성된 후 호출되는 메서드
3) doFilter : check 할 코드를 생성
- chain.doFilter : 다음 필터의 doFilter를 호출하거나 없으면 servlet의 service 메서드 호출
- chain.doFilter 위에 요청시 해야 할 코드 작성
- chain.doFilter 아래에 응답시 해야 할 코드 작성
3. 등록
- web.xml에 필터 사용 등록
1) Filter 객체 생성
<filter-class>
<filter-name> </filter-name>
<filter-class> </filter-class>
<init-param>
<param-name></param-name>
<param-value></param-value>
</init-param>
</filter-class>
<filter-mapping>
<filter-name></filter-name>
<url-pattern></url-pattern>
</filter-mapping>
Interceptor
- Spring
- 진행 순서를 가로 채서 작업
- Tomcat(Servlet) 에서 제공하는 Filter가 있음
- 요청 URL을 이용해서 걸러 주는 역할
1. Interceptor 생성
1) Interceptor 역할을 하는 클래스 선언, HandlerInterceptorAdator 클래스를 상속
2) 필요한 시점의 메서드를 오버라이딩 함, 모든 메서드를 오버라이딩 할 필요 없음
2. Interceptor 메서드
1) preHandle
- Controller 진입 전
2) postHandle
- Controller에서 Dispathcer Servlet으로 나갈 때
3) afterCompletion
- View(jsp)를 렌더링 후
3. Interceptor 설정
- servlet-context.xml에 작성
<interceptors>
<interceptor>
<mapping path="/**"/>
<exclude-mapping path="/notice/*"/>
<beans:bean class="com.winter.app.interceptors.TestInterceptor" id="testInterceptor"></beans:bean>
</interceptor>
</interceptors>
---------------------------------------------------------------------------------------------------------------
<beans:bean class="com.winter.app.interceptors.TestInterceptor" id="testInterceptor"></beans:bean>
<interceptors>
<interceptor>
<mapping path="/**"/>
<exclude-mapping path="/notice/*"/>
<beans:ref bean="testInterceptor"></beans:bean>
</interceptor>
</interceptors>
---------------------------------------------------------------------------------------------------------------
@Component
public class TestInterceptor extends HandlerInterceptorAdapter{...}
<interceptors>
<interceptor>
<mapping path="/**"/>
<exclude-mapping path="/notice/*"/>
<beans:ref bean="testInterceptor"></beans:bean>
</interceptor>
</interceptors>