import java.io.IOException;
import java.util.Date;
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 T08_ServletFilter implements Filter{
@Override
public void destroy() {
// 필터객체가 컨테이너에의해 서비스로부커 제거되기 전에 호출됨
System.out.println("T08_ServletFilter => destroy() 호출됨");
}
@Override
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain)
throws IOException, ServletException {
System.out.println("T08_ServletFilter => doFilter() 시작...");
// 클라이언트 IP주소 가져오기
String ipAddr = req.getRemoteAddr();
// 클라이언트 포트번호 가져오기
int port = req.getRemotePort();
System.out.println("IP주소 : "+ipAddr+"\n 포트번호 : "+port+"\n 현재시간 : "+new Date().toString());
// 필터체인(필터여러개)을 실행한다
chain.doFilter(req, resp);
// 필터체인 아래는 나가는 부분이래
System.out.println("T08_ServletFilter => doFilter() 끝... ");
}
@Override
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println("T08_ServletFilter => init() 호출됨");
// 초기화 파라미터 정보 가져오기
String initParam = filterConfig.getInitParameter("init-param");
System.out.println("init-param : "+initParam);
}
}
<filter>
<filter-name>T08</filter-name>
<filter-class>kr.or.ddit.basic.T08_ServletFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>T08</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>T01_SLC</servlet-name>
<servlet-class>kr.or.ddit.basic.T01_ServletLifeCycle</servlet-class>
**<load-on-startup>1</load-on-startup>**
</servlet>
<filter>
<filter-name>T08_ServletFilter</filter-name>
</filter>
<filter-mapping>
<filter-name>T08_ServletFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
import java.io.IOException;
import java.util.Date;
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 T08_ServletFilter2 implements Filter{
@Override
public void destroy() {
// 필터객체가 컨테이너에의해 서비스로부커 제거되기 전에 호출됨
System.out.println("T08_ServletFilter2 => destroy() 호출됨");
}
@Override
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain)
throws IOException, ServletException {
System.out.println("T08_ServletFilter2 => doFilter() 시작...");
// 서블릿 수행시간 계산하기
long startTime = System.currentTimeMillis();
// 필터체인(필터여러개)을 실행한다
chain.doFilter(req, resp);
System.out.println("서블릿 수행시간(ms) : "+(System.currentTimeMillis() - startTime));
System.out.println("T08_ServletFilter2 => doFilter() 끝... ");
}
@Override
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println("T08_ServletFilter2 => init() 호출됨");
// 초기화 파라미터 정보 가져오기
String initParam = filterConfig.getInitParameter("init-param");
System.out.println("init-param : "+initParam);
}
}
<filter>
<filter-name>T08_2</filter-name>
<filter-class>kr.or.ddit.basic.T08_ServletFilter2</filter-class>
</filter>
<filter-mapping>
<filter-name>T08_2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
http://localhost/ServletTest/T01_SLC 에 접속하면,
<filter>
<filter-name>T08</filter-name>
<filter-class>kr.or.ddit.basic.T08_ServletFilter</filter-class>
<init-param>
<param-name>init-param</param-name>
<param-value>파라미터값입니다</param-value>
</init-param>
</filter>
서블릿도 가능!
<servlet>
<servlet-name>T01_SLC</servlet-name>
<servlet-class>kr.or.ddit.basic.T01_ServletLifeCycle</servlet-class>
<init-param>
<param-name>init-param</param-name>
<param-value>서블릿 초기화</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>T01_SLC</servlet-name>
<url-pattern>/T01_SLC</url-pattern>
</servlet-mapping>
public class T03_ServletParameterTest extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//인코딩 주석처리
//req.setCharacterEncoding("UTF-8");
인코딩을 주석처리 했으니 이렇게 한글이름이 오류가 나게됨
인코딩 필터를 만들어서 넣어주기!
import java.io.IOException;
import java.security.spec.ECField;
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 CharacterEncodingFilter implements Filter {
private String encoding; //인코딩정보
@Override
public void destroy() {
// TODO Auto-generated method stub
}
@Override
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain)
throws IOException, ServletException {
System.out.println("인코딩 설정 정보"+ encoding);
req.setCharacterEncoding(encoding);
resp.setCharacterEncoding(encoding);
chain.doFilter(req, resp);
}
@Override
public void init(FilterConfig filterConfig) throws ServletException {
if(filterConfig.getInitParameter("endoing") == null) {
this.encoding = "UTF-8";
} else {
this.encoding = filterConfig.getInitParameter("encoding");
}
}
}
<filter>
<filter-name>CEF</filter-name>
<filter-class>kr.or.ddit.util.CharacterEncodingFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>CEF</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>