Filter 웹에서.xml config를 읽을 때 중국어 인코딩 문제

4719 단어 web.xml
우선, 웹.xml에서는 ASCII 범위를 초과한 문자를 표시하는 것을 권장하지 않습니다
그러나 하나의 축적으로 간단하게 예를 들면 다음과 같다. 그 핵심 코드는 바로 new String(String.getBytes(charset 1),charset 이다.2)
 1 public class SimpleFilter implements Filter {

 2     

 3     private boolean enable = false;

 4     

 5     public void init(FilterConfig config)

 6           throws ServletException{

 7         String enableString = config.getInitParameter("enable");

 8         if (enableString != null && enableString.equalsIgnoreCase("true")) {

 9             this.enable = true;

10         }

11         //         xml      ,       

12         //                ,             xml       ,    iso-8859-1    ?

13         String initParam = config.getInitParameter("ref");

14         try {

15             initParam = new String(initParam.getBytes("iso-8859-1"), "UTF-8");

16         } catch (UnsupportedEncodingException e) {

17             e.printStackTrace();

18         }

19         

20         System.out.println(this + ": init(), init-param = " + initParam);

21     }

22     public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)

23             throws IOException, ServletException{

24         if (this.enable)

25             System.out.println(this + ": doFilter()") ;

26         chain.doFilter(request, response);

27     }

28     public void destroy(){

29         // clean up

30     }

31     

32 }

좋은 웹페이지 즐겨찾기