java 過濾器(Filter)
阿新 • • 發佈:2018-12-01
java過濾器有很多用處比如:一些需要過濾器的情況:
(1)認證Filter
(2)日誌和稽核Filter
(3)圖片轉換Filter
(4)資料壓縮Filter
(5)密碼Filter
(6)令牌Filter
(7)觸發資源訪問事件的Filter
(8)XSLT Filter
(9)媒體型別鏈Filter
此篇文章用Filter來處理亂碼問題
使用Filter步驟處理亂碼步驟
1.建立實現Filter介面
2.先在web.xml中做好配置,filter 配置路徑/* (放在過濾器的第一個位置)
3.在filter中重寫getParameter(使用裝飾著設計模式)
4.在filter中, 對request進行加強.
實現Filter介面的EncodeFilter類
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; public class EncodeFilter implements Filter{ @Override public void destroy() { // TODO Auto-generated method stub System.out.println("Filter銷燬>>>>>>"); } //處理post提交亂碼 @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { // TODO Auto-generated method stub //強轉 System.out.println("對servlet進行攔截>>>>>>"); HttpServletRequest req=(HttpServletRequest)request; HttpServletResponse res=(HttpServletResponse)response; String method=req.getMethod(); res.setContentType("text/html;charset=utf-8"); System.out.println("這裡是filter>>>>>>>>>>>>>>>>>"); System.out.println("對servlet進行釋放>>>>>>"); chain.doFilter(req,res); } @Override public void init(FilterConfig arg0) throws ServletException { // TODO Auto-generated method stub System.out.println("Filter初始化工作>>>>>"); } }
裝飾者MyFilter.java強化了Parameter()和getParameterValues()方法
package Filter; import java.io.UnsupportedEncodingException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequestWrapper; public class MyFilter extends HttpServletRequestWrapper { private HttpServletRequest request; public MyFilter(HttpServletRequest request) { super(request); this.request = request; } // 重寫getParamter方法,處理實現get方式提交時的亂碼 @Override public String getParameter(String name) { // 如果為請求為Post提交 if (this.request.getMethod().equalsIgnoreCase("post")) { try { this.request.setCharacterEncoding("utf-8"); return this.request.getParameter(name); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } } String value = this.request.getParameter(name); if (value == null) { return null; } if(this.request.getMethod().equalsIgnoreCase("get")){ try { value = new String(value.getBytes("ISO-8859-1"), "utf-8"); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return value; } @Override public String[] getParameterValues(String name) { // 如果為請求為Post提交 if (this.request.getMethod().equalsIgnoreCase("post")) { try { this.request.setCharacterEncoding("utf-8"); System.out.println("編碼設定成功!>>>>>>>>>>>>>>>"); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } } String[] value =this.request.getParameterValues(name); if (value == null) { return null; } if(this.request.getMethod().equalsIgnoreCase("get")){ try { for(int i=0;i<value.length;i++){ value[i]= new String(value[i].getBytes("ISO-8859-1"), "utf-8"); } } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return value; } }