# filter
[ex]
web.xml
=======
<filter>
<display-name>A</display-name>
<filter-name>A</filter-name>
<filter-class>a.b.A</filter-class>
</filter>
<filter-mapping>
<filter-name>A</filter-name>
<url-pattern>/ABC</url-pattern>
</filter-mapping>
<filter>
<display-name>B</display-name>
<filter-name>B</filter-name>
<filter-class>c.d.B</filter-class>
</filter>
<filter-mapping>
<filter-name>B</filter-name>
<url-pattern>/ABC</url-pattern>
</filter-mapping>
<servlet>
<description></description>
<display-name>C</display-name>
<servlet-name>C</servlet-name>
<servlet-class>e.f.C</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>C</servlet-name>
<url-pattern>/ABC</url-pattern>
</servlet-mapping>
A.java
======
package a.b;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class A implements Filter {
public A(){System.out.println("A필터생성자");}
public void destroy(){System.out.println("A필터destroy");}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
System.out.println("A필터doFilter1");
request.setAttribute("a", "v1");
chain.doFilter(request, response);
System.out.println("A필터doFilter2");
PrintWriter out=response.getWriter();
out.println("<br/>A필터doFilter2");
}
public void init(FilterConfig fConfig) throws ServletException {
System.out.println("A필터init");
}
}
B.java
======
package c.d;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class B implements Filter {
public B(){System.out.println("B필터생성자");}
public void destroy(){System.out.println("B필터destroy");}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
System.out.println("B필터doFilter1");
request.setAttribute("b", "v2");
chain.doFilter(request, response);
System.out.println("B필터doFilter2");
PrintWriter out=response.getWriter();
out.println("<br/>B필터doFilter2");
}
public void init(FilterConfig fConfig) throws ServletException {
System.out.println("B필터init");
}
}
C.java
======
package e.f;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class C extends HttpServlet {
public C(){System.out.println("서블릿생성자");}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("서블릿doGet");
String s1=(String)request.getAttribute("a");
String s2=(String)request.getAttribute("b");
response.setContentType("text/html;charset=euc-kr");
PrintWriter out=response.getWriter();
out.println(s1+","+s2);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("서블릿doPost");
}
}
==> 톰캣을 가동했을때의 출력결과
A필터생성자
A필터init
B필터생성자
B필터init
서블릿생성자
==> url에서 다음과 같이 실행했을때의 출력결과
[웹브라우져출력결과]
v1,v2
B필터doFilter2
A필터doFilter2
[콘솔출력결과]
A필터doFilter1
B필터doFilter1
서블릿doGet
B필터doFilter2
A필터doFilter2
==> 톰캣을 중지시켰을때의 출력결과
A필터destroy
B필터destroy
'개발하자 > JSP&Servlet' 카테고리의 다른 글
sitemesh 활용하기 (decorator) (0) | 2015.03.26 |
---|---|
프로젝트 경로 pageContext.request.contextPath (0) | 2015.03.20 |
Bundle사용하여 언어바꾸기 (0) | 2015.03.19 |
텍스트필드 입력값 다음페이지로 넘기기 (0) | 2015.03.19 |
언어 클릭시 나라별 언어로 번역하기 EL사용 (0) | 2015.03.19 |