자바 필터 오류 방지,

5643 단어
  EncodingFilter   filter.EncodingFilter       charset   utf-8         EncodingFilter   /*  
package 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; /**  * 사용자 정의 문자 집합 필터, GET 방식 으로 폼 문자 집합 을 제출 하 는 문제 해결 *  * @author bhz  * @version 1.0.1   */public class EncodingFilter implements Filter {     private FilterConfig filterConfig;     private String chartSet = "UTF-8";     private static final String FORM_METHOD_POST = "POST";     private static final String FORM_METHOD_GET = "GET";     public FilterConfig getFilterConfig() {         return filterConfig;     }     public void setFilterConfig(FilterConfig filterConfig) {         this.filterConfig = filterConfig;     }     public void init(FilterConfig filterConfig) throws ServletException {         this.filterConfig = filterConfig;         String filterCharSet = filterConfig.getInitParameter("charset");         if (filterCharSet != null && filterCharSet.trim().length() != 0) {             this.chartSet = filterCharSet;         }     }     public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {         HttpServletRequest request = (HttpServletRequest) servletRequest;         HttpServletResponse response = (HttpServletResponse) servletResponse;         String formMethod = request.getMethod();         if (FORM_METHOD_POST.equalsIgnoreCase(formMethod)) {             request.setCharacterEncoding(this.chartSet);         } else if (FORM_METHOD_GET.equalsIgnoreCase(formMethod)) {             request = new RequestUrlWrapper(request, chartSet);         }         response.setCharacterEncoding(this.chartSet);         filterChain.doFilter(request, response);     } public void destroy() { this.filterConfig = null; } }
package filter; import javax.servlet.http.HttpServletRequestWrapper; import javax.servlet.http.HttpServletRequest; import java.io.UnsupportedEncodingException;/**  * RequestUrlWrapper.java  * 기능: 포장 요청 url * 클래스 이름: RequestUrlWrapper * ---------------------------------------------------  * V1.0.1    bhz    초기 버 전 */public class RequestUrlWrapper extends HttpServletRequestWrapper {     private String charSet = "UTF-8";     public static final String REQUEST_URL_ATTRIBUTE_NAME = "javax.servlet.forward.request_url.framework";     public RequestUrlWrapper(HttpServletRequest httpServletRequest) {         super(httpServletRequest);     }     public RequestUrlWrapper(HttpServletRequest httpServletRequest, String charSet) {         super(httpServletRequest);         this.charSet = charSet;     }     public String getParameter(String name) {         try {             super.setCharacterEncoding(this.charSet);         } catch (UnsupportedEncodingException e) {             e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.         }         String value = super.getParameter(name);         value = value == null ? null : convertCharSet(value);         return value;     }     public String convertCharSet(String target) {         try {             if ((!isGBK(target)) || "".equals(target)) {                 return new String(target.trim().getBytes("ISO-8859-1"), this.charSet);             } else {                 return target;             }         } catch (UnsupportedEncodingException e) {             e.printStackTrace();             return target;         }     }     public static String returnURL(HttpServletRequest request) throws Exception {         String url = request.getAttribute(REQUEST_URL_ATTRIBUTE_NAME).toString();         if (url == null) {             url = request.getRequestURL().toString();         }         return url == null ? "": url;     }     public static boolean isGBK(String string)             throws java.io.UnsupportedEncodingException {         byte[] bytes = string.replace('?', 'a').getBytes("ISO-8859-1");         for (int i = 0; i < bytes.length; i++) {             if (bytes[i] == 63) {                 return true;             }         }         return false;     }    /**      * gb 2312 에 있어 서 첫 번 째 바이트 의 위 치 는 0 이다.×81 ~ 0×FE, 꼬리 바이트 의 위 치 는 각각 0 이다.×40 에서 0×FE      *      * @param str      * @return      */    public static boolean isGB2312(String str) {         char[] chars = str.toCharArray();         boolean isGB2312 = false;         for (int i = 0; i < chars.length; i++) {             byte[] bytes = (""+ chars[i]).getBytes();             if (bytes.length == 2) {                 int[] ints = new int[2];                 ints[0] = bytes[0] & 0xff;                 ints[1] = bytes[1] & 0xff;                 if (ints[0] >= 0x81 && ints[0] <= 0xFE && ints[1] >= 0x40                         && ints[1] <= 0xFE) {                     isGB2312 = true;                     break;                 }             }         }         return isGB2312;     } }
 

좋은 웹페이지 즐겨찾기