public class MyFilter1 implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
// ํํฐ๋ฅผ ์ด๊ธฐํํ๋ ๊ณณ
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
// ํํฐ ๊ธฐ๋ฅ ์ํ ...
chain.doFilter(request, response);// chain์ผ๋ก ๋ค์ ํํฐ๋ก ์ฟผ๋ฆฌ ์ ๋ฌ
}
}
โ web.xml >>
<filter>
<filter-name>characterEncodingFilter</filter-name>
<filter-class>chap10.filter.CharacterEncodingFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<!-- ๋ชจ๋ ์์ฒญ์ ๋ํด ์ธ์ฝ๋ฉ์ ์ํ -->
<servlet-name>/*</servlet-name>
</filter-mapping>
โ CharacterEncodingFilter.java >>
public class CharacterEncodingFilter implements Filter {
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
System.out.println("์ธ์ฝ๋ฉ ํํฐ๋ฅผ ์ง๋๊ฐ");
// ๋ชจ๋ ์์ฒญ์ ๋ํ ์ธ์ฝ๋ฉ ์ฒ๋ฆฌ๋ฅผ ๋ฏธ๋ฆฌ ์งํํ ์ ์๋ค
request.setCharacterEncoding("EUC-KR");
response.setCharacterEncoding("EUC-KR");
chain.doFilter(request, response);
}
}
โ web.xml >>
<listener>
<listener-class>chap10.listener.MyServletRequestProcess</listener-class>
</listener>
<listener>
<listener-class>chap10.listener.ContextLoaderListener</listener-class>
</listener>
โ MyServletRequestProcess.java >>
public class MyServletRequestProcess implements ServletRequestListener {
@Override
public void requestInitialized(ServletRequestEvent sre) {
// ์์ฒญ ์ด๊ธฐํ ์์
//์์ฒญ ์์ ๋ถ๋ถ์ ์ด๋ฒคํธ ์ฒ๋ฆฌ ํ๊ณ ์ถ์ ๋ด์ฉ์ ์ ๋๋ค
// sre ๊ฐ์ฒด์ ServletRequest์ ServletContext๊ฐ ๋ชจ๋ ๋ค์ด์๋ค
System.out.println("ํ๋ผ๋ฏธํฐ : " + sre.getServletRequest().getParameter("flavor"));
// ํ๋ผ๋ฏธํฐ ๊บผ๋ด์ ์ฐ์ด๋ณด๊ธฐ
}
@Override
public void requestDestroyed(ServletRequestEvent sre) {
//์์ฒญ ๋ง๋ฌด๋ฆฌ ๋ถ๋ถ์ ์ด๋ฒคํธ ์ฒ๋ฆฌ ํ๊ณ ์ถ์ ๋ด์ฉ์ ์ ๋๋ค
}
}
โ ContextLoaderListener.java >>
public class ContextLoaderListener implements ServletContextListener{
@Override
public void contextInitialized(ServletContextEvent sce) {
System.out.println("ContextLoaderListener : ์ปจํ
์คํธ ๋ด๋ถ์ ๋ญ๊ฐ ๋ณํ๊ฐ ์์ต๋๋ค ...");
System.out.println("ContextLoaderListener : ๋ค์ ์ด๊ธฐํํฉ๋๋ค");
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
System.out.println("ContextLoaderListener : ServletContext Destroyed ... ");
}
}