1 16 package dlog4j; 17 18 import java.io.IOException ; 19 20 import javax.servlet.Filter ; 21 import javax.servlet.FilterChain ; 22 import javax.servlet.FilterConfig ; 23 import javax.servlet.ServletException ; 24 import javax.servlet.ServletRequest ; 25 import javax.servlet.ServletResponse ; 26 import javax.servlet.http.HttpServletRequest ; 27 import javax.servlet.http.HttpServletResponse ; 28 29 import dlog4j.proxy.RequestProxy; 30 import dlog4j.proxy.ResponseProxy; 31 import dlog4j.util.StringUtils; 32 33 37 public class UnicodeFilter implements Filter { 38 39 String encoding; 40 41 44 public void init(FilterConfig config) throws ServletException { 45 encoding = config.getInitParameter("encoding"); 46 if(StringUtils.isEmpty(encoding)) 47 encoding = "UTF-8"; 48 config.getServletContext().setAttribute("encoding", encoding); 49 } 50 51 56 public void doFilter(ServletRequest req, ServletResponse res,FilterChain chain) 57 throws IOException , ServletException 58 { 59 HttpServletRequest h_req = (HttpServletRequest )req; 60 HttpServletResponse response = (HttpServletResponse )res; 61 String accept = h_req.getHeader("accept"); 62 if(accept==null || accept.indexOf("text/vnd.wap.wml")==-1) 63 res.setContentType("text/html;charset=UTF-8"); 64 else{ 65 response.setContentType("text/vnd.wap.wml;charset=UTF-8"); 66 response.setHeader("Pragma","No-cache"); 67 response.setHeader("Cache-Control","no-cache"); 68 response.setDateHeader("Expires", 0); 69 } 70 HttpServletRequest p_req = RequestProxy.getProxy(req, encoding).getInstance(); 71 HttpServletResponse p_res = ResponseProxy.getProxy(response).getInstance(); 72 chain.doFilter(p_req, p_res); 73 } 74 75 78 public void destroy() { 79 } 80 81 } 82 | Popular Tags |