filter 설정 문자 인 코딩 형식 구현

2873 단어 자바
  • 하나의 filter
  • 실현
        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.http.HttpServletRequest;  
        import javax.servlet.http.HttpServletResponse;  
          
        public class SetCharacterEncodingFilter implements Filter { //   Filter    
         //          
         private String encode = null;  
         public void destroy(){  
            
         }  
           
         public void doFilter(ServletRequest req,ServletResponse resp,FilterChain chain)  
           throws IOException,ServletException{  
            
          //    
          HttpServletRequest request = (HttpServletRequest)req;  
          HttpServletResponse response = (HttpServletResponse)resp;  
            
          /* 
           *    web.xml                
           *     ,                   
           *          GBK 
           */  
          if(this.encode != null && !this.encode.equals("")){  
           request.setCharacterEncoding(this.encode);  
           response.setCharacterEncoding(this.encode);  
          }else{  
           request.setCharacterEncoding("GBK");  
           response.setCharacterEncoding("GBK");  
          }  
            
          /* 
           *   doFilter                  (servlet JSP  )。 
           * chain.doFilter          (     ),       servlet JSP  。 
           */  
          chain.doFilter(request, response);  
         }  
           
         public void init(FilterConfig config) throws ServletException{  
          //   web.xml                
          this.encode = config.getInitParameter("encode");  
         }  
           
           
        }  
  • 웹. xml 설정
  •       
          
          
           
           
          SetCharacterEncodingFilter  
            
            
           ssh.dlc.chp1.filter.SetCharacterEncodingFilter  
            
            
                
                           
           encode  
           GBK  
            
           
           
            
          SetCharacterEncodingFilter  
          *.do  
           
          

    좋은 웹페이지 즐겨찾기