1675
No
ProjectFilter.java
package sec01.ex06; import java.io.IOException; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.annotation.WebFilter; import javax.servlet.annotation.WebInitParam; @WebFilter( filterName="projectFilter", initParams= { @WebInitParam(name="enc", value="UTF-8") }) public class ProjectFilter implements Filter { String charset; public ProjectFilter() { } public void init(FilterConfig fConfig) throws ServletException { // 인코딩 초기 값을 추출하여 멤버변수인 charset에 저장한다. charset = fConfig.getInitParameter("enc"); System.out.println("init() 호출 ......... projectFilter"); } public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { //초기값을 추출하여 멤버 변수인 charst에 저장한 값으로 문자코드를 설정한다. request.setCharacterEncoding(charset); System.out.println(charset); System.out.println("ProjectFilter doFilter() 호출 전 ........."); chain.doFilter(request, response); System.out.println("ProjectFilter doFilter() 호출 후 ........."); } public void destroy() { System.out.println("destory () 호출 ......... projectFilter"); } }
web.xml
<filter> <filter-name>encode</filter-name> <filter-class>sec01.ex06.ProjectFilter</filter-class> <init-param> <param-name>enc</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>encode</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
댓글 ( 4)
댓글 남기기