본문 바로가기

개발하자/JSP&Servlet

filter 필터

# 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