JSP

 

 

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>

 

 

 

about author

PHRASE

Level 60  라이트

행복한 결혼생활을 위해 갈등이 생기는 것을 당연시 하고 양보하고 타협하라.

댓글 ( 0)

댓글 남기기

작성